convert double to int
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?
Related videos on Youtube
user496949
Updated on March 06, 2022Comments
-
user496949 about 2 years
What is the best way to convert a
double
to anint
? Should a cast be used?-
RPM1984 over 13 yearsdefine "best". depends if you want up/down rounding, etc.
-
Armen Tsirunyan over 13 years@RPM1984: Define
etc.
:) -
RPM1984 over 13 years@Armen - touche` :) Doesn't really matter anyway - Skeet is here, nothing else matters.
-
-
Armen Tsirunyan over 13 yearsDon't you think the third way is unnecessarily complex and slow?
-
Jeff Mercado over 13 yearsThe third will not work with doubles with fractional parts. e.g.,
double_value = 0.1
-
user496949 over 13 yearsI am wondering if in 64 bits machine, Convert.ToInt32 is not OK any more?
-
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 almost 11 yearsAnd doubles can be much huger than even a 64 bit int.
-
kris almost 8 yearsFor 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 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 beintVal = Convert.ToInt32(Math.Floor(dblVal));
-
kris almost 8 yearsObviously 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 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 almost 8 yearsThis a typically unsafe function because it accepts just anything.
-
Slai over 7 yearsInteresting that in VB.Net Double to Integer cast like
CInt(Double)
compiles to the defaultMath.Round(Double)
call andconv.ovf.i4
, but in C#(int)double
compiles toconv.i4
. In VB.NetCInt(1.5)
is 2, but in C#(int)1.5
is 1. -
lindexi about 7 yearsI think u can use Math.Floor.
-
Paul-Sebastian almost 3 yearsThis 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 toint
and truncate the fractional part or use something likeMath.Round
to get to the closestint
you're looking for.