Converting double to integer in Java
Categories:
Converting Double to Integer in Java: A Comprehensive Guide

Learn the various methods to convert double-precision floating-point numbers to integers in Java, understanding the implications of rounding, truncation, and potential data loss.
Converting a double
to an int
in Java is a common task, but it's crucial to understand the nuances involved. Unlike a simple type cast between primitive types like int
to long
, converting a floating-point number (double
) to an integer type (int
) involves handling the fractional part. This process can lead to data loss and requires careful consideration of how you want to handle the decimal values: whether to truncate, round up, round down, or round to the nearest integer.
Understanding the Core Problem: Data Loss
A double
can store a wide range of values, including fractional components (e.g., 3.14, 5.99). An int
, on the other hand, can only store whole numbers (e.g., 3, 5). When you convert a double
to an int
, the fractional part must be discarded. The method you choose dictates how that fractional part is discarded, which directly impacts the resulting integer value. Ignoring this can lead to unexpected behavior or incorrect calculations in your application.
flowchart TD A[Start with Double Value] --> B{How to handle fractional part?} B -->|Truncate (Cast)| C[Result: Floor for positive, Ceil for negative] B -->|Round to Nearest (Math.round)| D[Result: Standard rounding] B -->|Round Down (Math.floor)| E[Result: Always floor] B -->|Round Up (Math.ceil)| F[Result: Always ceil] C --> G[End with Integer Value] D --> G E --> G F --> G
Decision flow for converting double to integer in Java
Method 1: Simple Type Casting (Truncation)
The most straightforward way to convert a double
to an int
is by using a simple type cast (int)
. This method performs truncation, meaning it simply discards the fractional part of the double
value. For positive numbers, this is equivalent to Math.floor()
. For negative numbers, it's equivalent to Math.ceil()
. This is an important distinction to remember.
public class DoubleToIntCast {
public static void main(String[] args) {
double positiveDouble = 5.7;
double negativeDouble = -5.7;
double exactDouble = 5.0;
int castPositive = (int) positiveDouble; // Result: 5
int castNegative = (int) negativeDouble; // Result: -5
int castExact = (int) exactDouble; // Result: 5
System.out.println("Original: " + positiveDouble + ", Cast: " + castPositive);
System.out.println("Original: " + negativeDouble + ", Cast: " + castNegative);
System.out.println("Original: " + exactDouble + ", Cast: " + castExact);
}
}
Example of simple type casting from double to int
(int) -5.7
results in -5
, not -6
. If you need consistent 'round towards zero' behavior, this is suitable. If you need 'round down' (floor), use Math.floor()
.Method 2: Rounding to the Nearest Integer (Math.round())
If you need standard mathematical rounding (i.e., .5 and above rounds up, below .5 rounds down), Math.round()
is the method to use. Math.round()
returns a long
for double
inputs, so you'll need to cast the result to an int
. Remember that Math.round(x)
is equivalent to Math.floor(x + 0.5f)
for floats and Math.floor(x + 0.5d)
for doubles.
public class DoubleToIntRound {
public static void main(String[] args) {
double value1 = 5.7;
double value2 = 5.3;
double value3 = 5.5;
double value4 = -5.7;
double value5 = -5.3;
double value6 = -5.5;
int rounded1 = (int) Math.round(value1); // Result: 6
int rounded2 = (int) Math.round(value2); // Result: 5
int rounded3 = (int) Math.round(value3); // Result: 6
int rounded4 = (int) Math.round(value4); // Result: -6
int rounded5 = (int) Math.round(value5); // Result: -5
int rounded6 = (int) Math.round(value6); // Result: -5
System.out.println("Original: " + value1 + ", Rounded: " + rounded1);
System.out.println("Original: " + value2 + ", Rounded: " + rounded2);
System.out.println("Original: " + value3 + ", Rounded: " + rounded3);
System.out.println("Original: " + value4 + ", Rounded: " + rounded4);
System.out.println("Original: " + value5 + ", Rounded: " + rounded5);
System.out.println("Original: " + value6 + ", Rounded: " + rounded6);
}
}
Example of using Math.round() for double to int conversion
double
value is outside the range of int
(approximately -2 billion to +2 billion), Math.round()
will still return a long
. Casting this long
to int
will result in an overflow, leading to an incorrect value. Always consider the potential range of your double
values.Method 3: Rounding Down (Math.floor())
The Math.floor()
method returns the largest (closest to positive infinity) double
value that is less than or equal to the argument and is equal to a mathematical integer. This effectively rounds down towards negative infinity. You'll need to cast the double
result to an int
.
public class DoubleToIntFloor {
public static void main(String[] args) {
double value1 = 5.7;
double value2 = 5.3;
double value3 = -5.7;
double value4 = -5.3;
int floor1 = (int) Math.floor(value1); // Result: 5
int floor2 = (int) Math.floor(value2); // Result: 5
int floor3 = (int) Math.floor(value3); // Result: -6
int floor4 = (int) Math.floor(value4); // Result: -6
System.out.println("Original: " + value1 + ", Floor: " + floor1);
System.out.println("Original: " + value2 + ", Floor: " + floor2);
System.out.println("Original: " + value3 + ", Floor: " + floor3);
System.out.println("Original: " + value4 + ", Floor: " + floor4);
}
}
Example of using Math.floor() for double to int conversion
Method 4: Rounding Up (Math.ceil())
The Math.ceil()
method returns the smallest (closest to negative infinity) double
value that is greater than or equal to the argument and is equal to a mathematical integer. This effectively rounds up towards positive infinity. Similar to Math.floor()
, you'll need to cast the double
result to an int
.
public class DoubleToIntCeil {
public static void main(String[] args) {
double value1 = 5.7;
double value2 = 5.3;
double value3 = -5.7;
double value4 = -5.3;
int ceil1 = (int) Math.ceil(value1); // Result: 6
int ceil2 = (int) Math.ceil(value2); // Result: 6
int ceil3 = (int) Math.ceil(value3); // Result: -5
int ceil4 = (int) Math.ceil(value4); // Result: -5
System.out.println("Original: " + value1 + ", Ceil: " + ceil1);
System.out.println("Original: " + value2 + ", Ceil: " + ceil2);
System.out.println("Original: " + value3 + ", Ceil: " + ceil3);
System.out.println("Original: " + value4 + ", Ceil: " + ceil4);
}
}
Example of using Math.ceil() for double to int conversion
BigDecimal
for precise rounding and to avoid floating-point inaccuracies altogether.