Converting double to integer in Java
To convert a double to an int in Java, you can use the intValue()
method of the Double
class. Here is an example:
double d = 123.45;
int i = d.intValue();
Alternatively, you can use type casting to convert a double to an int. Here is an example:
double d = 123.45;
int i = (int) d;
Note that type casting a double to an int will cause any fractional part of the number to be truncated. For example, (int) 123.45
would yield 123
, and (int) 123.99
would yield 123
.