Java How to Convert Double to Int - Simple Examples
In Java, convert a
double to an int by using explicit casting like int result = (int) yourDouble; which truncates the decimal part.Examples
Input12.99
Output12
Input45.0
Output45
Input-3.7
Output-3
How to Think About It
To convert a double to an int, think about removing the decimal part because int only holds whole numbers. You do this by telling Java to treat the double as an int using casting, which cuts off anything after the decimal point without rounding.
Algorithm
1
Take the double value as input.2
Use explicit casting to convert the double to int.3
Store or use the resulting int value.Code
java
public class ConvertDoubleToInt { public static void main(String[] args) { double myDouble = 9.78; int myInt = (int) myDouble; // cast double to int System.out.println(myInt); // prints 9 } }
Output
9
Dry Run
Let's trace converting 9.78 to int through the code
1
Start with double value
myDouble = 9.78
2
Cast double to int
myInt = (int) 9.78 results in 9
3
Print int value
Output is 9
| Step | Value of myDouble | Value of myInt | Output |
|---|---|---|---|
| 1 | 9.78 | N/A | N/A |
| 2 | 9.78 | 9 | N/A |
| 3 | 9.78 | 9 | 9 |
Why This Works
Step 1: Casting removes decimals
Using (int) tells Java to convert the double to an int by dropping the decimal part.
Step 2: No rounding happens
Casting truncates the number, so 9.78 becomes 9, not 10.
Step 3: Result is stored as int
The result is stored in an int variable which can only hold whole numbers.
Alternative Approaches
Using Math.round() before casting
java
public class ConvertDoubleToInt { public static void main(String[] args) { double myDouble = 9.78; int myInt = (int) Math.round(myDouble); // rounds before casting System.out.println(myInt); // prints 10 } }
This rounds the double to the nearest whole number before converting, unlike simple casting which truncates.
Using Double.intValue() method
java
public class ConvertDoubleToInt { public static void main(String[] args) { Double myDouble = 9.78; int myInt = myDouble.intValue(); // converts to int by truncation System.out.println(myInt); // prints 9 } }
This uses the Double wrapper class method but also truncates decimals.
Complexity: O(1) time, O(1) space
Time Complexity
Casting a double to int is a simple operation done in constant time without loops.
Space Complexity
No extra memory is needed beyond the variables used, so space is constant.
Which Approach is Fastest?
Simple casting is fastest and most direct; using Math.round() adds a small overhead for rounding.
| Approach | Time | Space | Best For |
|---|---|---|---|
| Explicit casting (int) | O(1) | O(1) | Fast truncation without rounding |
| Math.round() then cast | O(1) | O(1) | When rounding is needed |
| Double.intValue() method | O(1) | O(1) | Using wrapper class, truncates decimals |
Use explicit casting
(int) to convert double to int and remember it cuts off decimals without rounding.Beginners often forget to cast explicitly and get a compile error or unexpected results.