Grails Cookbook - A collection of tutorials and examples

Float To Integer In Java

For most cases, we wish to work with Float than Integers because floats gives more precision and larger range of values. But sometimes there are reasons why we would want to work with integers with no decimal places. For example, when performance is very important, as working with integers is much faster than floats. This post will show some examples on how we could convert from float to Integer.

Check out the written examples of converting float to integer.


Explicit Cast

One easy way to convert float to integer is Explicit Cast. You can openly cast a value to another type using parenthesis. Check out the simple example of this process below:
/**
 * This is a simple program that converts java float to Integer.
 */
public class JavaFloatToIntExample1 {
   public static void main(String[] args) {
      float floatValue = 345.3f;
      int intValue = (int) floatValue;
      System.out.println("The int value is: " + intValue);
   }
}
Understand that the value in the variable floatValue is converted to integer because the decimal portion was basically removed. Check out the expected result below:
The int value is: 345
Remember that in casting, rounding numbers is not included. Take a look at the written sample below:
/**
 * This is a simple program that converts java float to int.
 */
public class JavaFloatToIntExample2 {
   public static void main(String[] args) {
      float floatValue = 225.7f;
      int intValue = (int) floatValue;
      System.out.println("The int value is: " + intValue);
   }
}
When we round of 225.7, the anticipated answer will be 226. This is not the case for casting, because as we have discussed, casting does not round the converted value of integer. The expected content of intValue is 225. Therefore, the ouput of the program is:
The int value is: 225

Using Float.intValue()

The class Float has a method that can help if the value is required to be converted. To get the integral component of the float object, a method should be invoked, that's where Float.intValue comes in. Check out the process below:

/**
 * This is a simple program that converts java float to int by using Float.intValue().
 */
public class JavaFloatToIntExample3 {
   public static void main(String[] args) {
      Float floatObject = 222.7f;
      int intValue = floatObject.intValue();
      System.out.println("The int value is: " + intValue);
   }
}

Take a look at the expected result below:
The int value is: 222

Handling Rounding

If rounding is necessary, you can use a static utility method in Math class. We know that rounding in programming isn't unusual, and it's also important. We can just use a static utility method in Math class. The purpose of Math.round is specified in the given name method - to round a float value to integer.

Check out the written example below:

/**
 * This is a simple program that converts java float to Int with rounding.
 */
public class JavaFloatToIntExample4 {
   public static void main(String[] args) {
      float floatValue = 7.6f;
      int intValue = Math.round(floatValue);
      System.out.println("The rounded int value is: " + intValue);
   }
}
The expected result should show 8, since it is the rounded value of 7.6. Check out the output below:
The rounded int value is: 8

Now, what happens if we try to round negative numbers. Check out the written example below:

/**
 * This is a simple program that converts a negative float value to Int with rounding.
 */
public class JavaFloatToIntExample5 {
   public static void main(String[] args) {
      float floatValue1 = -27.3f;
      float floatValue2 = -27.7f;
      int intValue1 = Math.round(floatValue1);
      int intValue2 = Math.round(floatValue2);
      System.out.println("The first rounded int value is: " + intValue1);
      System.out.println("The second rounded int value is: " + intValue2);
   }
}
If you answered -27 and -28, you are not incorrect.
Check out the result below:
The first rounded int value is: -27
The second rounded int value is: -28

Handling Ceiling

If you want to retrieve the whole number portion of the float value, rounding is unlikely the best thing to do. We can utilize the Math.ceil() method of the math utility class, one slightly problem with this is it will deliver you a double value. To which we can just cast it to integer value. Check out the written sample code:

/**
 * This is a simple program that converts java float to int with ceiling value.
 */
public class JavaFloatToIntExample6 {
   public static void main(String[] args) {
      float floatValue = 31.3f;
      double ceilingValue = Math.ceil(floatValue);
      int intValue = (int) ceilingValue;
      System.out.println("The int value is: " + intValue);
   }
}

The int value is: 32
We have discussed converting java float to integer using ceiling, now we will cover what happens when we involve negative numbers. Check out the written example below:
/**
 * This is a simple program that converts java float to int with ceiling of
 * negative values.
 */
public class JavaFloatToIntExample7 {
   public static void main(String[] args) {
      float floatValue1 = -35.3f;
      float floatValue2 = -35.7f;
      int intValue1 = (int) Math.ceil(floatValue1);
      int intValue2 = (int) Math.ceil(floatValue2);
      System.out.println("The first int value is: " + intValue1);
      System.out.println("The second int value is: " + intValue2);
   }
}
The first int value is: -35
The second int value is: -35