Read Also : Convert Signed Integer to String
1. Convert double to int using Typecasting : easiest way
2. Convert double to int using Math.round() function : use it to round of double to the nearest integer
3. Convert double to int using Double.intValue() method: use it when you have Double object
1. Convert double to int using Typecasting
In this example I will be converting double to int using typecasting. Typecasting is done by mentioning int in the brackets before the decimal value. The only downside of the typecasting is that it will truncate the decimal digits from the double value. It will not round of to the nearest value. For example, if we have double 99.99 then after typecasting the result will be 99 not 100. If you want 100 as an output then use Math.round() function./* Java Program to demonstrate conversion of double to int using Typecasting */ public class JavaHungry { // main method public static void main (String args[]) { // given double value double num = 39.99; // converting to int int result = (int) num; // showing the int value System.out.println(result); } }
Output : 39
2. Convert double to int using Math.round() function
In this example I will be converting double to int using Math.round() function. This function helps by rounding of the double to the nearest integer. For example, if we have double 99.99 then we will get 100 as int value./* Java Program to demonstrate conversion of double to int using Math.round() function */ public class JavaHungry { // main method public static void main (String args[]) { // given double value double num = 39.99; // converting to int int result = (int) Math.round(num); // showing the int value System.out.println(result); } }
Output : 40
3. Convert double to int using Double.intValue() method
In this example I will be using the wrapper Double class intValue() method to convert double to int in java. Just like typecasting, this method also truncates digits after decimal. For example, if we have double 99.99 then we will get 99 as int value./* Java Program to demonstrate conversion of double to int using Double.intValue() function */ public class JavaHungry { // main method public static void main (String args[]) { // given double value double num = 39.99; // Creating a wrapper around // the double value Double doubleObj = new Double(num); // converting to int int result = doubleObj.intValue(); // showing the int value System.out.println(result); } }
Output : 39
What happens if double is out of range for int value?
One point to note, if the value of the double is negative infinity or any value less than or equal to the Integer.MIN_VALUE, then the result is equal to the Integer.MIN_VALUE.Similarly,
If value of double is positive infinity or any value greater than or equal to Integer.MAX_VALUE, then the result is equal to the Integer.MAX_VALUE.
/* Java Program to demonstrate conversion of double to int where double is greater than Integer.MAX_VALUE */ public class JavaHungry { // main method public static void main (String args[]) { // given double value // larger than Integer.MAX_VALUE double num = 999999999999999999.99; // converting to int int result = (int) num; // showing the int value // It will print the Integer.MAX_VALUE System.out.println(result); } }
Output : 2147483647
In the above article, I have demonstrated 3 ways to convert double to int in java. Please mention in the comments if you have any questions. I will be happy to help.