Convert double to int in Java
In this Java core tutorial, we learn how to convert double value into int value in Java by different solutions.
How to cast double to int value
ConvertDoubleToIntExample1.java
public class ConvertDoubleToIntExample1 {
public static void main(String[] args) {
double value1 = 10.05;
int value2 = (int)value1;
System.out.println("double value: " + value1);
System.out.println("int value: " + value2);
}
}
double value: 10.05
int value: 10
How to use Math.round() method to convert double value to int
ConvertDoubleToIntExample2.java
public class ConvertDoubleToIntExample2 {
public static void main(String[] args) {
double value1 = 123.45;
int value2 = (int)Math.round(value1);
System.out.println("double value: " + value1);
System.out.println("int value: " + value2);
}
}
double value: 123.45
int value: 123
How to use Double.intValue() method to return int value from Double
ConvertDoubleToIntExample3.java
public class ConvertDoubleToIntExample3 {
public static void main(String[] args) {
double value1 = 789.24;
Double doubleValue = Double.valueOf(value1);
int value2 = doubleValue.intValue();
System.out.println("double value: " + value1);
System.out.println("int value: " + value2);
}
}
double value: 789.24
int value: 789
Happy Coding 😊