JavaDevNotes.com

Java Double To Int

It is convenient to use doubles on calculations because of the flexibility on precision. But once we are done with complex computation, we would wish to extract the integral part of the result. This post will show how to convert a Java double to int. Several examples will be given for extracting the result of double to int.

Convert Double to Int by Explicit Cast

The simplest way to convert a double to int is by casting explicitly. Casting can be done by having the type enclosed by parenthesis. Below is an example on how to do this:

/**
 * A Simple Program That Converts Java Double to Int.
 */
public class JavaDoubleToInt {
   public static void main(String[] args) {
      double doubleValue = 15.4;
      int intValue = (int) doubleValue;
      System.out.println("The int value is: " + intValue);
   }
}

The casting will extract the integral part of the double value and remove the decimal part. If we were to run the program above, the result will be:

The int value is: 15

Note that no rounding will be done. Consider the same code but casting the value 15.7. If we round 15.7, the answer is 16. But if we run the code below:
/**
 * A Simple Program That Converts Java Double to Int.
 */
public class JavaDoubleToInt {
   public static void main(String[] args) {
      double doubleValue = 15.7;
      int intValue = (int) doubleValue;
      System.out.println("The int value is: " + intValue);
   }
}

The result is still 15. This is because no round or any operation is done. Just plain extraction of the integral component of the double value.

The int value is: 15

Convert Double to Int with Rounding

If we wish to convert a Java double to int with rounding, the Math utility class has the static method round to help in this case. Math.round accepts a double value to round it to the next whole number. But since double has a big range, the result is a long value. We can just cast the resulting long value to an integer. Here is a simple example on this process:
/**
 * A Simple Program That Converts Java Double to Int with rounding.
 */
public class JavaDoubleToInt {
   public static void main(String[] args) {
      double doubleValue = 21.7;
      long longValue = Math.round(doubleValue);
      int intValue = (int) longValue;
      System.out.println("The int value is: " + intValue);
   }
}
Since the rounded value of 21.7 is 22, the result of the code above is shown below:
The int value is: 22

Don't get confused when rounding negative numbers. Here are some examples of converting negative double value to int.

/**
 * A Simple Program That Converts Java Double to Int with rounding of
 * negative values.
 */
public class JavaDoubleToInt {
   public static void main(String[] args) {
      double doubleValue1 = -21.3;
      double doubleValue2 = -21.7;
      int intValue1 = (int) Math.round(doubleValue1);
      int intValue2 = (int) Math.round(doubleValue2);
      System.out.println("The first int value is: " + intValue1);
      System.out.println("The second int value is: " + intValue2);
   }
}

Here are the results:

The first int value is: -21
The second int value is: -22

Convert Double to Int With Ceiling Value

If we wish to convert a Java double to int but by doing a ceiling and not rounding, the Math utility class also has another static method for this. The Math.ceiling will get the ceiling value of a number. The problem is that the result is still a double value. We can just cast the resulting value to int. Here is the sample code:

/**
 * A Simple Program That Converts  Java Double to Int with ceiling value.
 */
public class JavaDoubleToInt {
   public static void main(String[] args) {
      double doubleValue = 25.3;
      double ceilingValue = Math.ceil(doubleValue);
      int intValue = (int) ceilingValue;
      System.out.println("The int value is: " + intValue);
   }
}

The int value is: 26

Here are example of converting Java double to int using ceiling with negative numbers:

/**
 * A Simple Program That Converts Java Double to Int with ceiling of
 * negative values.
 */
public class JavaDoubleToInt {
   public static void main(String[] args) {
      double doubleValue1 = -52.3;
      double doubleValue2 = -52.7;
      int intValue1 = (int) Math.ceil(doubleValue1);
      int intValue2 = (int) Math.ceil(doubleValue2);
      System.out.println("The first int value is: " + intValue1);
      System.out.println("The second int value is: " + intValue2);
   }
}

Here is the output:

The first int value is: -52
The second int value is: -52

Convert Double to Int Using Double.intValue()

If the double you have is an object of class Double, the intValue() method can be invoked to get the integral component of the value. Here is an example:

/**
 * A Simple Program That Converts Java Double to Int by using Double.intValue().
 */
public class JavaDoubleToInt {
   public static void main(String[] args) {
      Double doubleObject = 11.7;
      int intValue = doubleObject.intValue();
      System.out.println("The int value is: " + intValue);
   }
}

And here is the result:

The int value is: 11