If you have a number of type double 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– How to convert double to int in Java.
Main point of concern while converting double to int in Java 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?
- Rounding- How rounding will happen for a double with decimal value when converted to integer.
So let’s see the options available for converting double to int in Java and how these concerns will be addressed.
Using Double.intValue() method
You can use intValue() method of the Double wrapper class to convert double to int. This method returns the value of this Double as an int after a narrowing primitive conversion.
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 type casting.
Double to int conversion 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 in Java 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.
Converting double to int example using Math.round()
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.
Following Java example covers the double value out of range scenarios.
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 Convert double to int in Java. If you have any doubt or any suggestions to make please drop a comment. Thanks!
>>>Return to Java Programs Page
Related Topics
You may also like-
No comments:
Post a Comment