Java Tutorial Java Advanced Java References

Java Math - round() Method



The Java round() method returns the value of argument rounded to its nearest integer. The method can be overloaded and it can take float and double arguments. In special cases it returns the following:

  • If the argument is NaN, the result is 0.
  • If the argument is negative infinity or any value less than or equal to the value of Integer.MIN_VALUE or Long.MIN_VALUE, the result is equal to the value of Integer.MIN_VALUE or Long.MIN_VALUE.
  • If the argument is positive infinity or any value greater than or equal to the value of Integer.MAX_VALUE or Long.MAX_VALUE, the result is equal to the value of Integer.MAX_VALUE or Long.MAX_VALUE.

Syntax

public static int round(float a)
public static long round(double a)

Parameters

a Specify a floating point value to be rounded.

Return Value

Returns the value of argument rounded to its nearest integer.

Exception

NA.

Example:

In the example below, round() method returns the value of argument rounded to its nearest integer.

public class MyClass {
 public static void main(String[] args) {
  System.out.println(Math.round(48.45));   
  System.out.println(Math.round(48.55));   
 }
}

The output of the above code will be:

48
49

❮ Java Math Methods