JavaDevNotes.com

Java Float To Int

In most cases where precision is important, we work with numbers having decimal point rather than whole numbers. For example, computation of points in 3D space - float will be more ideal for this. But later on, we need to convert back the float value to an integer when we want to know which coordinate on the screen it represents. This is just one scenario for the need of converting a Java float to int. There could be many more use cases. In case you also need it in your program, below are some examples on how to convert a float to int.

Convert Float to Int by Explicit Cast

Explicit cast is one of the simplest way on how to convert a float to it. Using parenthesis, you can explicitly cast a value to another type, provided that there is no conflict. Below is an example on how to cast a float value to int:

/**
 * A Simple Program That Converts Java Float to Int.
 */
public class JavaFloatToInt {
   public static void main(String[] args) {
      float floatValue = 225.3f;
      int intValue = (int) floatValue;
      System.out.println("The int value is: " + intValue);
   }
}

What this cast does is automatically converting the value to int by extracting the whole number part of the float value. The decimal portion is simply removed to get the resulting value. Hence running the code above will yield the result below:

The int value is: 225

Note that there is no rounding involved when casting. Consider the sample below:
/**
 * A Simple Program That Converts  Java Float to Int.
 */
public class JavaFloatToInt {
   public static void main(String[] args) {
      float floatValue = 225.7f;
      int intValue = (int) floatValue;
      System.out.println("The int value is: " + intValue);
   }
}
Even when the rounded value of 225.7 is 226, the intValue will get 225. This is because casting will not perform rounding. Hence the result will still be:
The int value is: 225

Convert Float to Int with Rounding

If rounding is important to your logic, you can use a static utility method in Math class. Math.round can be used to round a float value to int. Below is an example:
/**
 * A Simple Program That Converts Java Float to Int with rounding.
 */
public class JavaFloatToInt {
   public static void main(String[] args) {
      float floatValue = 7.7f;
      int intValue = Math.round(floatValue);
      System.out.println("The int value is: " + intValue);
   }
}
Since the rounded value of 7.7 is 8, the result should display 8. Here is the actual output:
The int value is: 8

Here is the behavior when rounding negative values:

/**
 * A Simple Program That Converts Java Float to Int with rounding.
 */
public class JavaFloatToInt {
   public static void main(String[] args) {
      float floatValue1 = -17.3f;
      float floatValue2 = -17.7f;
      int intValue1 = Math.round(floatValue1);
      int intValue2 = Math.round(floatValue2);
      System.out.println("The first int value is: " + intValue1);
      System.out.println("The second int value is: " + intValue2);
   }
}

The converted negative float value to int are -17 and -18, as shown below:

The first int value is: -17
The second int value is: -18

Convert Float to Int With Ceiling Value

Rounding may not always be the best operation when retrieving the whole number part of a float value. Sometimes the ceiling is more appropriate. This is simple using the ceil static method of the math utility class. The problem is that the result Math.ceil() is a double value. We can just cast the resulting value to int. Here is the sample code:

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

The int value is: 12

Here is an example of converting Java float to int using ceiling with negative numbers:

/**
 * A Simple Program That Converts Java Float to Int with ceiling of
 * negative values.
 */
public class JavaFloatToInt {
   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);
   }
}

Here is the output:

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

Convert Float to Int Using Float.intValue()

If the value needed to be converted is of the class Float, then this class has a method that can help in this use case. The Float.intValue() method can be invoked to get the integral component of the float object. Here is an example:

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

And here is the result:

The int value is: 122