Tuesday, November 16, 2021

Convert double to int in Java

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-

  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 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

  1. Convert String to float in Java
  2. Convert double to String in Java
  3. Convert float to int in Java
  4. Fibonacci Series Program in Java
  5. Converting String to Enum Type in Java

You may also like-

  1. Print Odd-Even Numbers Using Threads And wait-notify Java Program
  2. How to Create PDF From XML Using Apache FOP
  3. How to Convert Date And Time Between Different Time-Zones in Java
  4. Java Program to Convert a File to Byte Array
  5. Ternary Operator in Java With Examples
  6. Exception Propagation in Java Exception Handling
  7. ArrayList in Java With Examples
  8. Java ReentrantLock With Examples

No comments:

Post a Comment