java double to integer

By Chaitanya Singh | Filed Under: Java Conversion

In this tutorial, we will learn how to convert double to int in Java. As we know double value can contain decimal digits (digits after decimal point), so when we convert double value with decimal digits to int value, the decimal digits are truncated. In this java tutorial, we will see three ways to convert a double value to int. Out of these 3 ways there is one way in which you can round of the double value to nearest integer value.

1. Convert double to int using typecasting
2. Convert double to int using Math.round() – This ensures that the decimal digits double value is rounded of to the nearest int value.
3. Convert double to int using Double.intValue()

1. Java – double to int conversion using type casting

To typecast a double value to integer, we mention int keyword in the brackets before the decimal double value. The only downside of conversion using typecasting is that the double value is not rounded of, instead the digits after decimal are truncated. We can solve this issue by using Math.round() method which we have discussed in the second example.

Output: Here is the output of the above program –

2. Java – Convert double to int using Math.round()

In this example we are converting the double value to integer value using Math.round() method. This method rounds of the number to nearest integer. As you can see in the output that the double value 99.99 is rounded of to the nearest int value 100 .

Output:

Convert double to int in Java using Double.intValue()

In this example we are using Wrapper class Double . This is same as typecasting method, where the digits after decimal are truncated.

Output:

Given a Double real number, the task is to convert it into Integer in Java.

Examples:

    Using typecasting: This technique is a very simple and user friendly.

Syntax:

Example:

Syntax:

Example:

Syntax:

Example:

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the «Improve Article» button below.

Suppose you have a double primitive variable 4.444 and you want to convert it to the integer value 4 , how do you that in Java? Since double is bigger data type than int , you can simply downcast double to int in Java. double is 64-bit primitive value and when you cast it to 32-bit integer, anything after the decimal point is lost. Btw, type casting doesn’t do any rounding or flooring, which means if you have 9.999999 and while casting to int you are expecting 10 then you would be disappointed, casting will give you just 9. If you need 10 then you need to use Math.round() method to first round the double value to the nearest integer and then truncate decimals.

As we have seen, while converting float to int, the Math.round() method is overloaded and the version which accepts double returns a long primitive value. If you need an int primitive value then you need to further cast long to int in Java. By the way, that’s not the only way.

You can also convert a double primitive variable to Double wrapper object and then call the intValue() method to get a corresponding int value. In this article, I’ll show you a couple of examples to convert double to int in Java.

double to int in Java — Typecasting

The easiest way to convert a double to int in Java is by type casting but it works only when your requirement is just to get r >int , it needs to be down-casted as shown below:

If you remember, floating point numbers are by default double in Java, so you don’t need any suffix, unlike representing float values. As I said, casting get rid of anything after decimal so even 6.999 gets converted into 6.

double to int — Math.round()

If you want to convert floating point double value to nearest int value then you should use the Math.round() method. It accepts a double value and converts into nearest long value by adding 0.5 and truncating decimal points. Once you got the long value, you can cast it to int again, as shown below:

As you can see that double value is translated to nearest integer by using Math.round() method. You can read Head First Java 2nd Edition or Core Java for the Impatient to learn more about rounding in Java.

Double to int in Java — Double.intValue()

There is another way to convert a double value to int in Java, by using wrapper >java.lang.Double method intValue() . You can first use auto-boxing to convert double primitive to Double and then just call intValue() method, this will return an equivalent integer value, as shown below :

This works but it’s like going around the world, first convert primitive to object and then back to primitive. It suits better if you already have a Double object. Btw, this method is similar to casting and doesn’t provide rounding or flooring facility.

You can see that all the conversion is direct conversion, no rounding or flooring is applied. See Java How to Program by Dietel and Dietel to learn more about flooring and rounding in Java.

Important points

1) By default, all floating point numbers are double in Java, hence you don’t need any prefix, but when you declare a floating point value with type float, you must use the «f» or «F» as suffix e.g.

float PIE = 3.14f;

2) By down casting a double to an int you can remove anything after decimal point. Though this will not apply any rounding or flooring.

3) The Math.round() method from java.lang.Math class converts a double value to the nearest long value, if you want int, you can cast long to int value.

4) You can also convert a double to int in Java by first converting it into an object of corresponding wrapper >intValue() method e.g. Double.intValue()

Here is the summary of all three ways to convert a double to an int value in Java:

That’s all about how to convert double to int in Java. Simplest way to convert double to int in Java is by downcasting it. This will return you the non-fractional part of the number as an int, but it doesn’t do rounding, so even 9.999 will be converted to 9. If you are looking to convert double to nearest integer e.g. 9.999 to 10 then you should use Math.random() method. It rounds the double value to the nearest integer by adding 0.5 and then truncating decimal value. Though, this method return long, which needs to be further downcast into an int. Another, third way to convert double to int is by using Double.intValue() method, which is best if you already have a Double object, but you can also use auto-boxing to convert double primitive to Double object and then call this method.

Other data type conversion articles you may like

  • How to convert String to Integer in Java? (tutorial)
  • How to convert float to int in Java? (tutorial)
  • How to convert String to float in Java? (tutorial)
  • The best way to convert Integer to String in Java? (example)
  • How to convert a char value to String in Java? (tutorial)
  • How to convert String to ASCII values in Java? (example)
  • How to convert a long to String in Java? (article)

Thank you for reading this tutorial so far, if you like then please share with your friends and colleagues, it makes a lot of difference.

Оцените статью
SoftLast
Добавить комментарий