Java

How to Convert double to int in Java

In Java, “double” and “int” are the most popular primitive data types, where double is a 64-bit floating-point number and integer, also known as “int”, is a 32-bit integer representing signed two’s complement. While programming in Java, you may need to convert double to int where the input given by the user is in floating points, and an integer value is required for further processing.

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:

int i = (int) d;

 
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:

double d = 856.187;

 
Next, we will convert “d” into an int by using typecasting:

int i = (int)d;

 
After performing the specified operation, we will print the converted value:

System.out.println("The value in data type Double " + d + " is converted in Integer " + i);

 

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:

int i = new Double(d).intValue();

 
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:

int i = new Double(d).intValue();

 
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:

System.out.println("The value in data type Double " + d + " is converted in Integer " + i);

 

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:

int i = (int)Math.round(d);

 
Example

We will utilize the same “d” variable and convert its value to “int” using the Math.round() method:

int i = (int)Math.round(d);

 
At the end, print the integer value on the console:

System.out.println("The value in data type Double " + d + " is converted in Integer " + i);

 

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.

About the author

Farah Batool

I completed my master's degree in computer science. I am an academic researcher and love to learn and write about new technologies. I am passionate about writing and sharing my experience with the world.