user496949
user496949

Reputation: 86055

convert double to int

What is the best way to convert a double to an int? Should a cast be used?

Upvotes: 211

Views: 514728

Answers (13)

zangetsu
zangetsu

Reputation: 11

Use the inbuilt Convert class. It offers all type of conversion methods whether from int to double, double to float or byte etc.

int i= Convert.ToInt32(500*3.142456778)

Upvotes: 1

Dorathoto
Dorathoto

Reputation: 236

There is no better. It really depends on what you want, performance, accuracy, etc.

But see differences https://dotnetfiddle.net/kwqF2M

double testeDouble_min = 12.3456;
double testeDouble_max = 12.8456;
double emptyDouble;

int i01_min = Convert.ToInt32(testeDouble_min); //12
int i01_max = Convert.ToInt32(testeDouble_max); //13
//var erro = Convert.ToInt32(emptyDouble); //error  

int i02_min = (int)testeDouble_min; //12
int i02_max = (int)testeDouble_max; //12
//var erro = (int)emptyDouble; //error  

var styles = System.Globalization.NumberStyles.Integer | System.Globalization.NumberStyles.AllowDecimalPoint;
var provider = new System.Globalization.CultureInfo("en-US");
bool success = int.TryParse(testeDouble_min.ToString(), styles, provider, out int i03_min); //0

_ = int.TryParse(testeDouble_max.ToString(),  out int i03_max); //0 because it has decimal place, if it didn't have ok

int i04_min = (int)Math.Round(testeDouble_min, MidpointRounding.AwayFromZero); //12
int i04_max = (int)Math.Round(testeDouble_max, MidpointRounding.AwayFromZero); //13

About IL there are no big differences between (int) or Convert.ToInt32

var dd = 12.3;
int a = (int)dd;

result:

    IL_0000: nop
    IL_0001: ldc.r8 12.3
    IL_000a: stloc.0
    IL_000b: ldloc.0
    IL_000c: conv.i4
    IL_000d: stloc.1
    IL_000e: ret

(int) in theory less instructions tend to be faster, but I doubt anyone can measure that.

And

var dd = 12.3;
int b = Convert.ToInt32(dd);

result:

    IL_0000: nop
    IL_0001: ldc.r8 12.3
    IL_000a: stloc.0
    IL_000b: ldloc.0
    IL_000c: call int32 [System.Private.CoreLib]System.Convert::ToInt32(float64)
    IL_0011: stloc.1
    IL_0012: ret

Upvotes: 2

RAM
RAM

Reputation: 485

None of the code samples exhibit the normal behaviour a school kid would expect (that halves are rounded up). What most people would expect is is this:

double d = 1.5;
int i = (int)Math.Round(d, MidpointRounding.AwayFromZero);

Upvotes: 3

mathewsun
mathewsun

Reputation: 373

int myInt = (int)Math.Ceiling(myDouble);

Upvotes: 0

majed
majed

Reputation: 537

The best way is to simply use Convert.ToInt32. It is fast and also rounds correctly.

Why make it more complicated?

Upvotes: 9

Ruth
Ruth

Reputation: 15

label8.Text = "" + years.ToString("00") + " years";

when you want to send it to a label, or something, and you don't want any fractional component, this is the best way

label8.Text = "" + years.ToString("00.00") + " years";

if you want with only 2, and it's always like that

Upvotes: -1

Singleton
Singleton

Reputation: 3679

I think the best way is Convert.ToInt32.

Upvotes: -1

mariana soffer
mariana soffer

Reputation: 1853

Here is a complete example

class Example 
{    
  public static void Main() 
  {    
    double x, y; 
    int i; 

    x = 10.0; 
    y = 3.0; 

    // cast double to int, fractional component lost (Line to be replaced) 
    i = (int) (x / y); 
    Console.WriteLine("Integer outcome of x / y: " + i); 
  }    
}

If you want to round the number to the closer integer do the following:

i = (int) Math.Round(x / y); // Line replaced

Upvotes: 1

kst
kst

Reputation: 1518

My ways are :

 - Convert.ToInt32(double_value)
 - (int)double_value
 - Int32.Parse(double_value.ToString());

Upvotes: 3

Armen Tsirunyan
Armen Tsirunyan

Reputation: 132974

if you use cast, that is, (int)SomeDouble you will truncate the fractional part. That is, if SomeDouble were 4.9999 the result would be 4, not 5. Converting to int doesn't round the number. If you want rounding use Math.Round

Upvotes: 46

anishMarokey
anishMarokey

Reputation: 11397

Convert.ToInt32 is the best way to convert

Upvotes: 6

Jon Skeet
Jon Skeet

Reputation: 1499770

You can use a cast if you want the default truncate-towards-zero behaviour. Alternatively, you might want to use Math.Ceiling, Math.Round, Math.Floor etc - although you'll still need a cast afterwards.

Don't forget that the range of int is much smaller than the range of double. A cast from double to int won't throw an exception if the value is outside the range of int in an unchecked context, whereas a call to Convert.ToInt32(double) will. The result of the cast (in an unchecked context) is explicitly undefined if the value is outside the range.

Upvotes: 286

Jeff Mercado
Jeff Mercado

Reputation: 134801

Yeah, why not?

double someDouble = 12323.2;
int someInt = (int)someDouble;

Using the Convert class works well too.

int someOtherInt = Convert.ToInt32(someDouble);

Upvotes: 55

Related Questions