Cast Double to Integer in Java
I see three possibilities. The first two cut off the digits, the last one rounds to the nearest Integer.
double d = 9.5;
int i = (int)d;
//i = 9
Double D = 9.5;
int i = Integer.valueOf(D.intValue());
//i = 9
double d = 9.5;
Long L = Math.round(d);
int i = Integer.valueOf(L.intValue());
//i = 10
A Double
is not an Integer
, so the cast won't work. Note the difference between the Double
class and the double
primitive. Also note that a Double
is a Number
, so it has the method intValue
, which you can use to get the value as a primitive int
.
I think it's impossible to understand the other answers without covering the pitfalls and reasoning behind it.
You cannot directly cast an Integer
to a Double
object. Also Double
and Integer
are immutable objects, so you cannot modify them in any way.
Each numeric class has a primitive alternative (Double
vs double
, Integer
vs int
, ...). Note that these primitives start with a lowercase character (e.g. int
). That tells us that they aren't classes/objects. Which also means that they don't have methods. By contrast, the classes (e.g. Integer
) act like boxes/wrappers around these primitives, which makes it possible to use them like objects.
Strategy:
To convert a Double
to an Integer
you would need to follow this strategy:
- Convert the
Double
object to a primitivedouble
. (= "unboxing") - Convert the primitive
double
to a primitiveint
. (= "casting") - Convert the primitive
int
back to anInteger
object. (= "boxing")
In code:
// starting point
Double myDouble = Double.valueOf(10.0);
// step 1: unboxing
double dbl = myDouble.doubleValue();
// step 2: casting
int intgr = (int) dbl;
// step 3: boxing
Integer val = Integer.valueOf(intgr);
Actually there is a shortcut. You can unbox immediately from a Double
straight to a primitive int
. That way, you can skip step 2 entirely.
Double myDouble = Double.valueOf(10.0);
Integer val = Integer.valueOf(myDouble.intValue()); // the simple way
Pitfalls:
However, there are a lot of things that are not covered in the code above. The code-above is not null-safe.
Double myDouble = null;
Integer val = Integer.valueOf(myDouble.intValue()); // will throw a NullPointerException
// a null-safe solution:
Integer val = (myDouble == null)? null : Integer.valueOf(myDouble.intValue());
Now it works fine for most values. However integers have a very small range (min/max value) compared to a Double
. On top of that, doubles can also hold "special values", that integers cannot:
- 1/0 = +infinity
- -1/0 = -infinity
- 0/0 = undefined (NaN)
So, depending on the application, you may want to add some filtering to avoid nasty Exceptions.
Then, the next shortcoming is the rounding strategy. By default Java will always round down. Rounding down makes perfect sense in all programming languages. Basically Java is just throwing away some of the bytes. In financial applications you will surely want to use half-up rounding (e.g.: round(0.5) = 1
and round(0.4) = 0
).
// null-safe and with better rounding
long rounded = (myDouble == null)? 0L: Math.round(myDouble.doubleValue());
Integer val = Integer.valueOf(rounded);
Auto-(un)boxing
You could be tempted to use auto-(un)boxing in this, but I wouldn't. If you're already stuck now, then the next examples will not be that obvious neither. If you don't understand the inner workings of auto-(un)boxing then please don't use it.
Integer val1 = 10; // works
Integer val2 = 10.0; // doesn't work
Double val3 = 10; // doesn't work
Double val4 = 10.0; // works
Double val5 = null;
double val6 = val5; // doesn't work (throws a NullPointerException)
I guess the following shouldn't be a surprise. But if it is, then you may want to read some article about casting in Java.
double val7 = (double) 10; // works
Double val8 = (Double) Integer.valueOf(10); // doesn't work
Integer val9 = (Integer) 9; // pure nonsense
Prefer valueOf:
Also, don't be tempted to use new Integer()
constructor (as some other answers propose). The valueOf()
methods are better because they use caching. It's a good habit to use these methods, because from time to time they will save you some memory.
long rounded = (myDouble == null)? 0L: Math.round(myDouble.doubleValue());
Integer val = new Integer(rounded); // waste of memory
You need to explicitly get the int value using method intValue() like this:
Double d = 5.25;
Integer i = d.intValue(); // i becomes 5
Or
double d = 5.25;
int i = (int) d;