Wednesday, 28 September 2016

Converting double to int - Java Program

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 -

  1. You need a Double class object to use intValue() method.
  2. 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

  1. Converting string to float - Java Program
  2. Converting double to string - Java Program
  3. Converting float to int - Java program
  4. Fibonacci series program in Java
  5. Armstrong number

You may also like -

>>>Go to Java Programs page

No comments:

Post a Comment