Skip to content

Converting double to integer in Java

To convert a double to an int in Java, you can use the intValue() method of the Double wrapper class. Here is an example:


java
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:


java
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.

If you need to round the value instead of truncating it, you can use Math.round():


java
double d = 123.45;
int i = (int) Math.round(d);

Do you find this helpful?

Dual-run preview — compare with live Symfony routes.