Grails Cookbook - A collection of tutorials and examples

Double To Integer In Java

Now we use double for it's conviency on calculations and flexibility in precision. Like most of the time, some of us wants to take out the integral part of the output once we are finished with complicated computations. And now, we will discuss the process of converting java double to integer. Check out the examples given for extracting the integral portion from the output:

Double To Int Using Explicit Cast

Just a quick reminder: that casting works when you enclosed a data type within the open and close parenthesis, this is one way to convert a double to integer. Check out the process below:
/**
 * This is a simple program that converts java double to integer.
 */
public class JavaDoubleToIntegerExample1 {
   public static void main(String[] args) {
      double doubleValue = 20.4;
      int intValue = (int) doubleValue;
      System.out.println("The int value is: " + intValue);
   }
}

Notice that the data type integer is in the parenthesis prior to the variable doubleValue. Casting is done when the integral portion of the number is set aside, and the decimal part has been removed. And the expected result of the program above is:

The int value is: 20

Now this time, let's try with a different value. Say, 15.7 is in the variable doubleValue, will the answer be just plainly 15? or 16? Check out the program below:
/**
 * This simple program that converts java double to integer.
 */
public class JavaDoubleToIntegerExample2 {
   public static void main(String[] args) {
      double doubleValue = 15.7;
      int intValue = (int) doubleValue;
      System.out.println("The int value is: " + intValue);
   }
}
The int value is: 15
Notice that the output is still 15, this is because casting is only capable of removing the decimal part of the value and not rounding the number.

Double to Int Using 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:

Take note that if you have a double and its an object of class Double, there is a method you can invoke in order to acquire the integral component of the output and that is: intValue.

/**
 * This is a simple program that converts java double to integer by using Double.intValue().
 */
public class JavaDoubleToInt {
   public static void main(String[] args) {
      Double doubleObject = 91.7;
      int intValue = doubleObject.intValue();
      System.out.println("The int value is: " + intValue);
   }
}
Check out the expected result below:
The int value is: 91

Double To Int with Rounding

There is a way to round a value while doing casting, and that is tapping the static method round that the Math utility class have. Math.round's purpose is rounding the accepted double value to the next whole number, but there's a catch, the result is a long value because double has a big range, to which we could just cast it easily to integer.


Check out the written simple example on how to do this:

/**
 * This is a simple program that converts java souble to integer with rounding.
 */
public class JavaDoubleToIntegerExample3 {
   public static void main(String[] args) {
      double doubleValue = 51.7;
      long longValue = Math.round(doubleValue);
      int intValue = (int) longValue;
      System.out.println("The rounded int value is: " + intValue);
   }
}
The rounded int value is: 52
Understand that the same process applies when rounding negative numbers.
/**
 * This is a simple program that converts java double to integer with rounding of
 * negative values.
 */
public class JavaDoubleToIntegerExample4 {
   public static void main(String[] args) {
      double doubleValue1 = -51.3;
      double doubleValue2 = -51.7;
      int intValue1 = (int) Math.round(doubleValue1);
      int intValue2 = (int) Math.round(doubleValue2);
      System.out.println("The first rounded int value is: " + intValue1);
      System.out.println("The second rounded int value is: " + intValue2);
   }
}
Check out the result below:
The first rounded int value is: -51
The second rounded int value is: -52

Double to Integer With Ceiling Value

Now let's talk about ceiling, and how to convert a java double to integer through this. Note that this is another static method that the Math utility class has. We can use Math.ceiling if we want to acquire ceiling value of a number, and expect the output is still a double value. Upon getting the result, cast the value to integer.


Check out the written program below:

/**
 * This is a simple program that converts java double to integer with ceiling value.
 */
public class JavaDoubleToIntegerExample5 {
   public static void main(String[] args) {
      double doubleValue = 100.3;
      double ceilingValue = Math.ceil(doubleValue);
      int intValue = (int) ceilingValue;
      System.out.println("The int value is: " + intValue);
   }
}
The int value is: 101
We got 101 as the resulting value because of the converted number within the variable doubleValue in which it is converted to integer. Put it this way, 100.3 has the maximum number 101, neither we can't go beyond 101 nor lower than 100.3.


Let's include the negative numbers while we convert a double to integer using ceiling.

/**
 * This is a simple program that converts java double to integer with ceiling of
 * negative numbers.
 */
public class JavaDoubleToInt {
   public static void main(String[] args) {
      double doubleValue1 = -40.3;
      double doubleValue2 = -40.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);
   }
}
Check out the expected result below:
The first int value is: -40
The second int value is: -40