If you have a double number and you have a requirement to show number with out fractional part then one of the option is to
convert that double type to int and display the value, that’s what this post is about – **Converting double to int**.

Main point of concern here are -

**Range**; as range of double is much more than int so if you have a double value which will be out of range if converted to int what should happen?- How rounding will happen for a double with decimal value when converted to integer.

So let’s see the options available and how these concerns will be addressed.

__Using Double.intValue() method__

public class DoubleToInt { public static void main(String[] args) { Double d = new Double(89.876); int val = d.intValue(); System.out.println("int value " + val); } }

__Output__

int value 89

Here two things to note are -

- You need a Double class object to use intValue() method.
- Rounding didn’t happen, method just removed the fractional part.

Same result can be achieved by explicitly **typecasting**.

__Using typecasting__

public class DoubleToInt { public static void main(String[] args) { double d = 89.876; int val = (int)d; System.out.println("int value " + val); } }

__Output__

int value 89

Here note that you need double primitive type not an object, if you are using explicit casting. *But the rounding still
didn’t happen*.

__Using Math.round() method__

As you have seen above methods of converting double to int are just giving the whole part of the number but mostly you will also like to do rounding. For that you can use Math.round method which takes double as argument and returns the value of the argument rounded to the nearest int value.

__Example code__

public class DoubleToInt { public static void main(String[] args) { //Double d = new Double(89.876); double d = 89.876; int val = (int)Math.round(d); System.out.println("int value " + val); } }

__Output__

int value 90

As you can see number is rounded off to the nearest integer value. *You still need to typecast to int because Math.round(double d)
returns long*.

__If double value out of range__

- If the argument is negative infinity or any value less than or equal to the value of Integer.MIN_VALUE, the result is equal to the value of Integer.MIN_VALUE.
- If the argument is positive infinity or any value greater than or equal to the value of Integer.MAX_VALUE, the result is equal to the value of Integer.MAX_VALUE.

__Example code__

public class DoubleToInt { public static void main(String[] args) { Double d = new Double(8989767565656.876); int val = d.intValue(); System.out.println("int value " + val); double d1 = 8989767565656.876; int val1 = (int)d1; System.out.println("int value-1 " + val1); int val2 = (int)Math.round(d1); System.out.println("int value-2 " + val2); } }

__Output__

int value 2147483647 int value-1 2147483647 int value-2 401015129

Here you can see since double value’s range is much bigger Integer.MAX_VALUE is returned for the first two cases, in the third case since there are two conversions first from double to long and then casting to int so value is different.

That's all for this topic **Converting double to int - Java Program**. If you have any doubt or any
suggestions to make please drop a comment. Thanks!

__Related Topics__

**You may also like - **