Java – Convert Float to Int
To convert a float to an int in Java, use an explicit cast such as (int) value when you want to discard the fractional part. Use Math.round(value) when you want to round the float to the nearest integer instead.
The important difference is how the fractional part is handled. Casting truncates toward zero, while Math.round() rounds according to the numeric value.
1. Convert float to int using explicit casting
A Java float can represent fractional values, while an int represents whole numbers. Converting from float to int is therefore a narrowing primitive conversion and requires an explicit cast.
The syntax for converting a float value to int is:
int result = (int) floatValue;
The cast does not round the number. It removes the fractional part by truncating toward zero. For example, 16.8564f becomes 16, while -16.8564f becomes -16.
In the following example, we have a variable f which is of float type. We shall typecast f to integer value and store in variable n.
Java Program
/**
* Java Program - Convert Float to Integer
*/
public class FloatToInt {
public static void main(String[] args) {
float f = 16.8564f;
int n = (int) f;
System.out.println(n);
}
}
Output
16
The cast converts 16.8564f to 16. It does not inspect the decimal digits to decide whether the value should move to the next integer.
Float-to-int casting truncates toward zero
Truncation toward zero is especially important for negative values. Casting -8.9f to int produces -8, not -9.
public class FloatToIntCasting {
public static void main(String[] args) {
float a = 8.9f;
float b = -8.9f;
System.out.println((int) a);
System.out.println((int) b);
}
}
8
-8
This behavior differs from rounding down. For negative numbers, truncating toward zero and taking the mathematical floor can produce different results.
2. Convert float to int using Math.round()
Math.round(float) returns an int representing the closest integer according to Java’s rounding rule. This is appropriate when the fractional part should affect the result instead of simply being discarded.
In this example, we shall convert floating point numbers with different precision and convert to int, to understand the rounding functionality.
Java Program
/**
* Java Program - Convert Float to Integer
*/
public class FloatToInt {
public static void main(String[] args) {
float f;
int n;
f = 16.8564f;
n = Math.round(f);
System.out.println(n);
f = 16.4235f;
n = Math.round(f);
System.out.println(n);
}
}
Output
17
16
Here, 16.8564f becomes 17, whereas 16.4235f becomes 16. This differs from casting, which would convert both values to 16.
Difference between (int) casting and Math.round() for float values
| Float value | (int) value | Math.round(value) |
|---|---|---|
16.8f | 16 | 17 |
16.2f | 16 | 16 |
-16.8f | -16 | -17 |
-16.2f | -16 | -16 |
Use casting when you specifically want to remove the fractional part. Use Math.round() when you want the fractional part to determine which nearby integer is selected.
How Math.round() handles float values ending in .5
For a float argument, Java defines Math.round() in terms of the mathematical floor of the value plus 0.5. This is worth noting for negative halfway values.
public class FloatRounding {
public static void main(String[] args) {
System.out.println(Math.round(4.5f));
System.out.println(Math.round(-4.5f));
}
}
5
-4
Therefore, Math.round(-4.5f) returns -4. Do not assume that every halfway value is rounded away from zero.
Round a float down to an int with Math.floor()
If the requirement is to always round toward negative infinity, use Math.floor(). The method returns a double, so an explicit conversion is required when the final result must be an int.
public class FloatFloorToInt {
public static void main(String[] args) {
float a = 8.9f;
float b = -8.9f;
int x = (int) Math.floor(a);
int y = (int) Math.floor(b);
System.out.println(x);
System.out.println(y);
}
}
8
-9
Notice the difference for the negative value: casting -8.9f directly produces -8, whereas applying Math.floor() first produces -9.
Round a float up to an int with Math.ceil()
To round toward positive infinity, use Math.ceil(). Like Math.floor(), Math.ceil() returns a double.
public class FloatCeilToInt {
public static void main(String[] args) {
float a = 8.1f;
float b = -8.9f;
int x = (int) Math.ceil(a);
int y = (int) Math.ceil(b);
System.out.println(x);
System.out.println(y);
}
}
9
-8
Choose Math.ceil() only when the intended rule is to move toward positive infinity. It is not a replacement for ordinary nearest-integer rounding.
Float values outside the int range during casting
A float can represent magnitudes outside the range of Java’s 32-bit int. During a narrowing conversion, a finite value that is too large becomes Integer.MAX_VALUE, while a finite value that is too small becomes Integer.MIN_VALUE.
public class FloatToIntRange {
public static void main(String[] args) {
float large = 1.0e20f;
float small = -1.0e20f;
System.out.println((int) large);
System.out.println((int) small);
}
}
2147483647
-2147483648
If preserving the original magnitude matters, check the range before performing the conversion instead of relying on this boundary behavior.
Converting NaN and infinity from float to int
Java also defines conversions for special floating-point values. Casting Float.NaN to int produces 0. Positive infinity converts to Integer.MAX_VALUE, and negative infinity converts to Integer.MIN_VALUE.
public class SpecialFloatToInt {
public static void main(String[] args) {
System.out.println((int) Float.NaN);
System.out.println((int) Float.POSITIVE_INFINITY);
System.out.println((int) Float.NEGATIVE_INFINITY);
}
}
0
2147483647
-2147483648
In application code, it is often clearer to test special values with Float.isNaN() or Float.isFinite() before converting them.
Choose the correct float-to-int conversion for the required rounding rule
| Required result | Java approach |
|---|---|
| Discard the fractional part | (int) value |
| Nearest integer | Math.round(value) |
| Round toward negative infinity | (int) Math.floor(value) |
| Round toward positive infinity | (int) Math.ceil(value) |
The choice should be based on the numeric rule required by the program. Casting and rounding are different operations and can return different integers from the same float.
Java float-to-int conversion summary
Use an explicit (int) cast when you want to truncate a float toward zero. Use Math.round() when you need the nearest integer, Math.floor() when you need to round down toward negative infinity, and Math.ceil() when you need to round up toward positive infinity.
In this Java Tutorial, we learned how to write Java programs to convert an Float to Integer.
TutorialKart.com