convert double to int

393,955

Solution 1

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.

Solution 2

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

Solution 3

Yeah, why not?

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

Using the Convert class works well too.

int someOtherInt = Convert.ToInt32(someDouble);

Solution 4

Convert.ToInt32 is the best way to convert

Solution 5

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

Why make it more complicated?

Share:
393,955

Related videos on Youtube

user496949
Author by

user496949

Updated on March 06, 2022

Comments

  • user496949
    user496949 about 2 years

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

    • RPM1984
      RPM1984 over 13 years
      define "best". depends if you want up/down rounding, etc.
    • Armen Tsirunyan
      Armen Tsirunyan over 13 years
      @RPM1984: Define etc. :)
    • RPM1984
      RPM1984 over 13 years
      @Armen - touche` :) Doesn't really matter anyway - Skeet is here, nothing else matters.
  • Armen Tsirunyan
    Armen Tsirunyan over 13 years
    Don't you think the third way is unnecessarily complex and slow?
  • Jeff Mercado
    Jeff Mercado over 13 years
    The third will not work with doubles with fractional parts. e.g., double_value = 0.1
  • user496949
    user496949 over 13 years
    I am wondering if in 64 bits machine, Convert.ToInt32 is not OK any more?
  • Joren
    Joren over 13 years
    @user: The size of an int is always 32 bits, regardless of whether you're using a 32 or 64 bit machine.
  • Adrian Ratnapala
    Adrian Ratnapala almost 11 years
    And doubles can be much huger than even a 64 bit int.
  • kris
    kris almost 8 years
    For C# noobs like me: both Math and Convert are part of System. So the complete answer looks like this : intVal = System.Convert.ToInt32(System.Math.Floor(dblVal));
  • Jon Skeet
    Jon Skeet almost 8 years
    @user1290746: That would work, but you'd usually just have using System; at the top of the file, at which point it could just be intVal = Convert.ToInt32(Math.Floor(dblVal));
  • kris
    kris almost 8 years
    Obviously that is obvious to anyone familiar with C#. And someone searching for an answer about type conversion is possibly/likely completely unfamiliar with the language - that was the case for me anyway - so thanks for your extra info.
  • Jon Skeet
    Jon Skeet almost 8 years
    @user1290746: But in that case, I don't think the right solution is to advise people to use the fully-qualified names - the better solution is to learn about using directives.
  • Anton Shepelev
    Anton Shepelev almost 8 years
    This a typically unsafe function because it accepts just anything.
  • Slai
    Slai over 7 years
    Interesting that in VB.Net Double to Integer cast like CInt(Double) compiles to the default Math.Round(Double) call and conv.ovf.i4, but in C# (int)double compiles to conv.i4. In VB.Net CInt(1.5) is 2, but in C# (int)1.5 is 1.
  • lindexi
    lindexi about 7 years
    I think u can use Math.Floor.
  • Paul-Sebastian
    Paul-Sebastian almost 3 years
    This is most certainly not the best way and you should probably retract your answer. By using ToString you're newing up a lot of other costly resources behind the scenes, like a string formatter and parser which are meant for special purposes. Learn from the answers given here. The closest to best way is to cast to int and truncate the fractional part or use something like Math.Round to get to the closest int you're looking for.