Round Float Values
public static int round(float a)This method converts a float number to the nearest int value. It rounds the given float value to get the nearest int value. For example:
float num1 = 14.4f; float num2 = 14.6f; int roundedNum1 = Math.round(num1); int roundedNum2 = Math.round(num2); System.out.println(roundedNum1); System.out.println(roundedNum2);The code above will have the following output based on rounding the float argument:
14 15
If we make it negative values, for example:
float num1 = -14.4f; float num2 = -14.6f; int roundedNum1 = Math.round(num1); int roundedNum2 = Math.round(num2); System.out.println(roundedNum1); System.out.println(roundedNum2);The result will be:
-14 -15For the edge cases, the documentation mentions the following:
float num = Float.NaN; System.out.println(num); int roundedNum = Math.round(num); System.out.println(roundedNum);Will have this output:
NaN 0
float num = -1/0.0f; int roundedNum = Math.round(num); System.out.println(num); System.out.println(roundedNum);We produce a negative infinity number by dividing a negative number by 0. The rounded value of that is the Integer.MIN_VALUE. The output of the code therefore will be:
-Infinity -2147483648
float num = 1/0.0f; int roundedNum = Math.round(num); System.out.println(num); System.out.println(roundedNum);We produce infinity number by dividing a positive number by 0. The rounded value of that is the Integer.MAX_VALUE. The output of the code above will be:
Infinity 2147483647
Round Double Values
public static long round(double a)Similar to above, we use double values when precision of float is not enough. Hence the method is overloaded with a signature that accepts a double value. Since double can contain bigger values than float, the returned value now is long instead of int.
double num1 = 52147483647.4d; double num2 = 52147483647.6d; long roundedNum1 = Math.round(num1); long roundedNum2 = Math.round(num2); System.out.println(roundedNum1); System.out.println(roundedNum2);The code above proves that we can go beyond the Integer.MAX_VALUE since we added one more digits to the input. In fact the max value returned will now be Long.MAX_VALUE. The output of the code above is:
52147483647 52147483648
If we make it negative values, for example:
double num1 = -52147483647.4d; double num2 = -52147483647.6d; long roundedNum1 = Math.round(num1); long roundedNum2 = Math.round(num2); System.out.println(roundedNum1); System.out.println(roundedNum2);Similarly, negative values can hold a lower value than Integer.MIN_VALUE. The lowest possible returned value now is Long.MIN_VALUE. Output of the code is shown below:
-52147483647 -52147483648
Edge cases are also similar:
double num = Double.NaN; long roundedNum = Math.round(num); System.out.println(num); System.out.println(roundedNum);the output is:
NaN 0
double num = Double.NEGATIVE_INFINITY; long roundedNum = Math.round(num); System.out.println(num); System.out.println(roundedNum);the output is:
-Infinity -9223372036854775808
double num = Double.POSITIVE_INFINITY; long roundedNum = Math.round(num); System.out.println(num); System.out.println(roundedNum);the output is:
Infinity 9223372036854775807