HOWTO · Java
How to Convert Double to Int in Java
Learn when to cast a double to int, use Math.round(), or call Double.intValue() in Java.
On this page
To convert a Java double to an int, cast it with (int) value when you want to truncate toward zero, use (int) Math.round(value) when you want the nearest whole number, or call value.intValue() when the value is a Double object. For example, with 12.9, casting and intValue() produce 12, while rounding then casting produces 13.
Choose the method based on the behavior you need: casting is the simplest for truncation, Math.round() rounds to a nearby integer, and Double.intValue() is useful when you already have a wrapper object. Each method returns an int in the examples below; the Math.round() call itself returns a long before the explicit cast.
How to Convert a Double to an Int in Java Using Type Casting
A cast converts a primitive double to the primitive type int. Java truncates the fractional part toward zero; it does not round down. For example, 12.9 becomes 12. The conversion can also lose information because an int cannot retain a fractional part. See the guide to type casting in Java for the broader distinction between widening and narrowing conversions.
The syntax is (int) doubleValue. Here is a complete example you can run as DoubleToIntConversion.java:
Run this example:
public class DoubleToIntConversion {
public static void main(String[] args) {
double value = 12.9;
int result = (int) value;
System.out.println("double: " + value);
System.out.println("int: " + result);
}
}
The supplied runtime output confirms that the example produces 12 for the int result. The conversion truncates toward zero for values on either side of zero; for instance, -12.9 becomes -12, not -13.
How to Convert a Double to an Int in Java Using the round() Method
Use Math.round() when you want the nearest whole number rather than truncation. For a double argument, Math.round() returns a long, so cast that result explicitly when the destination must be an int: (int) Math.round(value). For example, 12.9 rounds to 13, while 12.3 rounds to 12.
The cast to int has its own range behavior. If the rounded long is outside the int range, converting it to int does not report an error; the result follows Java’s narrowing conversion rules. Keep the value in a long if the full result may exceed the range of int.
Here is a complete example you can run as RoundDoubleToInt.java:
public class RoundDoubleToInt {
public static void main(String[] args) {
double value = 12.9;
long rounded = Math.round(value);
int result = (int) rounded;
System.out.println("double: " + value);
System.out.println("rounded long: " + rounded);
System.out.println("int: " + result);
}
}
The supplied runtime output confirms that Math.round() produces 13L, which then converts to the int value 13.
How to Convert a Double to an Int in Java Using the intValue() Method
intValue() is an instance method on the Double wrapper class, not on the primitive double type. Call it on a Double object to obtain an int. Like a primitive cast, it truncates toward zero rather than rounding, so a Double containing 12.9 yields 12.
The syntax is wrapper.intValue(). Java can box a primitive double into a Double when assigning it, as shown here. Save this complete program as DoubleWrapperToInt.java:
public class DoubleWrapperToInt {
public static void main(String[] args) {
Double value = 12.9;
int result = value.intValue();
System.out.println("Double: " + value);
System.out.println("int: " + result);
}
}
The supplied runtime output confirms that Double.intValue() gives 12 for 12.9.
If your next step is parsing text rather than converting an existing numeric value, see how to convert a hexadecimal string to an int in Java.