Grails Cookbook - A collection of tutorials and examples

Java Math.Round()

The java.lang.Math class contains many static method for performing various math related functions. One useful utility is the Round() method, which is used to round a float or double number to the closest whole number representation. This post will give explain and give examples on how to use the Java Math.Round() method.

Math.Round Examples

The Java.Round has two implementations. We could pass either a float or double number to convert to the nearest whole numbers. Here are the two signatures:

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
-15

For the edge cases, the documentation mentions the following:
  • If the argument is NaN, the result is 0. - since a float number can have the value Nan, the converted value will be 0 for such case. Example, consider this code:
    float num = Float.NaN;
    System.out.println(num);
    int roundedNum = Math.round(num);
    System.out.println(roundedNum);
    
    Will have this output:
    NaN
    0
    
  • If the argument is negative infinity or any value less than or equal to the value of Integer.MIN_VALUE, the result is equal to the value of Integer.MIN_VALUE. - The value of Integer.MIN_VALUE is -2147483648, which is the lowest value that this method can return. Example:
    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
    
  • If the argument is positive infinity or any value greater than or equal to the value of Integer.MAX_VALUE, the result is equal to the value of Integer.MAX_VALUE. - The value of Integer.MAX_VALUE is 2147483647, which is the biggest value that this method can return.
    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:

  • If the argument is NaN, the result is 0. - The behavior is similar as described above, example code:
    double num = Double.NaN;
    long roundedNum = Math.round(num);
    System.out.println(num);
    System.out.println(roundedNum);
    
    the output is:
    NaN
    0
    
  • If the argument is negative infinity or any value less than or equal to the value of Long.MIN_VALUE, the result is equal to the value of Long.MIN_VALUE. - Similarly, the lowest value that this method will return is Long.MIN_VALUE or -9223372036854775808. See this code sample:
    double num = Double.NEGATIVE_INFINITY;
    long roundedNum = Math.round(num);
    System.out.println(num);
    System.out.println(roundedNum);
    
    the output is:
    -Infinity
    -9223372036854775808
    
  • If the argument is positive infinity or any value greater than or equal to the value of Long.MAX_VALUE, the result is equal to the value of Long.MAX_VALUE. - And as expected, the highest value that this method will return is Long.MAX_VALUE or 9223372036854775807. See this code sample:
    double num = Double.POSITIVE_INFINITY;
    long roundedNum = Math.round(num);
    System.out.println(num);
    System.out.println(roundedNum);
    
    the output is:
    Infinity
    9223372036854775807