How to convert double to int in Java
In this article, we will learn how to convert a double value to an int in Java examples. We will discuss briefly two ways of converting double to integer namely typecasting and rounding and after that will see some example code for that.
1. Difference in conversion with Typecasting and Math.round()
1.1 Typecasting
Typecasting is a way to convert one type to another type in java. For example like converting double to int or vice versa.
int number = (int) 2.33; = 2
1.2 Math.round()
It’s a way to round off a number to its nearest integer value. For example
int number = (int) Math.round(2.33) = 2 int number = (int) Math.round(2.55) = 3
1.3 Differences in Typecasting and Math.round
As shown above, typecasting will convert the double value to an integer by just dropping any number after the decimal points. Whereas Math.round() converts the double value with decimal points to its nearest long value. Then we can use int within the brackets to cast it to the integer value.
2. Convert using Math.round()
Below is the example of how we can convert the double value to an integer using Math.round()
Example code using Math.Round()
public class MathRound { public static void main(String[] args) { int lowerRounding = (int) Math.round(3.24); int upperRounding = (int) Math.round(3.50); System.out.println("Rounding to the lower nearest integer value "+lowerRounding); System.out.println("Rounding to the upper nearest integer value "+upperRounding); } }
In the above example, we have two double values, which we convert to the integer value using the Math.round(). In the first case, we use the number 3.24 and the nearest number to that is 3. On the other hand for the second case, we have the value as 3.50 for which the nearest number is 4. The execution of the same is shown below with the output
3. Convert using Double.intValue()
There is another way of converting the double value to an integer using method intValue() provided by the Double wrapper class. Below is the example is shown below for the same
Example code using Double.intValue()
public class DoubleIntValue { public static void main(String[] args) { Double firstDoubleValue = 3.24; Double secondDoubleValue = 3.50; int firstIntegerValue = firstDoubleValue.intValue(); int secondIntegerValue = secondDoubleValue.intValue(); System.out.println("First Integer value with double.intValue "+firstIntegerValue); System.out.println("Second Integer value with double.intValue "+secondIntegerValue); } }
In the above example, we have two double values, which we convert to the integer value using the method intValue(). It is similar to the typecasting and yields the same result as typecasting. But it is a bit longer than the normal typecasting, as it needs to auto-boxed first and then need to call the intValue method. In both, cases, as shown above, it, returns the value 3 as it just drops the number after decimal points. The execution of the same is shown below with the output
4. Convert using the Typecasting
Below is the example of how we can convert the double value to an integer using Typecasting as discussed above
Example code using typecasting
public class TypeCasting { public static void main(String[] args) { double firstdoubleValue = 3.24; double secondDoubleValue = 3.50; int firstIntegerValue = (int) firstdoubleValue; int secondIntegerValue = (int) secondDoubleValue; System.out.println("First Integer value with typecasting "+firstIntegerValue); System.out.println("Second Integer value with typecasting "+secondIntegerValue); } }
In the above example we have two double values, which we convert to the integer value using typecasting. In both the cases as shown above it returns the value 3 as it drops the number after decimal points irrespective of it values. The execution of the same is shown below with the output.
5. Summary
In this article we discussed about ways to convert a double value to integer using typecasting, Math.round() method and intValue() method of double wrapper class. We also discussed the example for each of different ways for the conversion.
6. Download the source code
This was the tutorial about how to convert double to int in Java with examples.
You can download the full source code of this example here: How to convert double to int in Java