Convert float to nearest integer in Java
In this post, we will show we how to convert a float to the nearest integer in Java using typecasting and Math.round() method. We will also explain the advantages of each and provide some code examples to help us understand better.
Converting a float to the nearest integer means rounding the decimal part of the float to the closest whole number. For example, when x = 5.60f, the output should be 6 and for x = -5.60f, the output should be -6.
1. Using Typecasting
Typecasting allows us to convert one data type to another by explicitly specifying the desired type in parentheses. For example, to convert a float to an int, we can write (int) before the float value. However, typecasting does not round the float value to the nearest integer, but it truncates the value instead and drops everything after the decimal point. For example, when x = 5.60f, (int) x will give 5 instead of 6. For example:
|
1 2 3 4 5 6 7 8 9 |
class Main { public static void main(String[] args) { float x = 5.60f; int y = (int)x; System.out.println("y = " + y); // y = 5 } } |
But there is a workaround. For rounding the positive values, we can use the expression (int)(x + 0.5), and for rounding the negative values, we can use the expression (int)(x - 0.5), where x is the given floating-point number.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
class Main { public static void main(String[] args) { float x = 5.60f; int y; if (x >= 0.0f) { y = (int) (x + 0.5); // `x` is positive } else { y = (int)(x - 0.5); // `x` is negative } System.out.println("y = " + y); // y = 6 } } |
The advantage of using this method is that it is simple and fast, as no function call is required. It also works for any floating point value regardless of size or sign.
2. Using Math.round() method
The standard solution is to use the Math class, which provides the round() method that returns the closest integer to the given decimal number. It follows the standard rounding rules: if the decimal part is equal to or greater than 0.5, it rounds up; otherwise, it rounds down. For example, when x = 5.60f, Math.round(x) will give 6 and when x = 5.40f, Math.round(x) will give 5. Here is an example:
|
1 2 3 4 5 6 7 8 9 10 |
class Main { public static void main(String[] args) { System.out.println(Math.round(5.40f)); // 5 System.out.println(Math.round(5.60f)); // 6 System.out.println(Math.round(-5.60f)); // -6 System.out.println(Math.round(-5.40f)); // -5 } } |
The advantage of using this method is that it rounds the float value to the nearest integer, which may be what we want or expect. It also follows the standard rounding rules, which are consistent and predictable. However, it may be slower than typecasting, as it requires an additional method call and calculation.
That’s all about converting float to the nearest integer in Java.
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)