This post will teach the procedure of converting double to int in Java.
How to Convert double to int in Java?
Converting a number from double to int is considered a common conversion task in programming. As mentioned earlier, the double data type has a decimal value. When these numbers are converted to integers, the decimal digits are rounded to the closest number.
In Java, you can convert data type double to int:
-
- Using “TypeCasting”
- Using “Double.intValue()” method
- Using “Math.round()” method
Let’s discuss each of them one by one!
Method 1: Convert double to int in Java by Using TypeCasting
Typecasting is a process of converting one data type to another. It is primarily utilized by Java developers to test the data variable. You can also use it for converting double data type variables to int.
Syntax
The syntax for converting double to int in Java using TypeCasting method is given as:
“d” is the variable that stores a value of data type “double”. The keyword “int” with the parenthesis like “(int)” indicates that the mentioned double type “d” variable is typecasted into an integer, and the resultant value will be stored in “i”.
Example
First, we will create a double type variable “d” and assign it the following value:
Next, we will convert “d” into an int by using typecasting:
After performing the specified operation, we will print the converted value:
The output shows the integer value “856” after truncating the decimal points:
Method 2: Convert double to int in Java by Using Double.intValue() Method
Double.intValue() is another method used to convert the number from double data type to int. The “intValue()” is the method of the Java “Double” Wrapper class. This method converts the double value into the nearest integer value and returns an integer. It works similar to typecasting.
Syntax
The syntax for converting a number in data type double to int by using “Double.intValue()” method is as follows:
Have a look at the following example to know more about the given method.
Example
We will convert the value of the already created “d” variable using the “Double.intValue()” method:
The above-given code will create an object of the “Double” Wrapper class and pass a value of “d” in it, and then call the “intValue()” method.
After converting, print the resultant integer value on the console using the “System.out.println()” method:
Output
Method 3: Convert double to int in Java by Using Math.round() Method
Another method to convert the double to int in Java is the “Math.round()”. It belongs to the Java “Math” class. It takes a double value and rounds it into the nearest integer value.
Syntax
The syntax for the “Math.round()” method is:
Example
We will utilize the same “d” variable and convert its value to “int” using the Math.round() method:
At the end, print the integer value on the console:
You can see the output, which shows the double value “850.171” is rounded off to “850”:
We provided all the necessary instructions related to converting double to int in Java.
Conclusion
To convert double to int in Java, there are three different methods: Typecasting, Math.round(), and Double.intValue(). Typecasting and Double.intValue() methods approximately work the same when utilized for double to int conversion. Typecasting is performed explicitly. In contrast, the Math.round() method rounds off the double value to an integer value. In this post, we have discussed the procedure for converting double to int in Java with examples.