That is, if the specified value is 5.8, the closest value that is equal to the mathematical integer is 6.0. And, for value 5.4, the closest value that is equal to mathematical integer is 5.0.
The syntax of the rint()
method is:
Math.rint(double value)
Note: The rint()
method is a static method. Hence, we can call the method directly using the class name Math
.
rint() Parameters
- arg - argument whose closest value that is equal to mathematical integer is returned
rint() Return Values
- returns the closest value to arg that is equal to the mathematical integer
Example: Java Math.rint()
class Main {
public static void main(String[] args) {
// Math.rint()
// value greater than 5 after decimal
System.out.println(Math.rint(1.878)); // 2.0
// value less than 5 after decimal
System.out.println(Math.rint(1.34)); // 1.0
// value equal to 5 after decimal
System.out.println(Math.rint(1.5)); // 2.0
// value equal to 5 after decimal
System.out.println(Math.rint(2.5)); // 2.0
}
}
In the above example, notice the two expressions,
// returns 2.0
Math.rint(1.5)
// returns 2.0
Math.rint(2.5)
Here, in both cases, the value after the decimal is equal to 5. However,
- for 1.5 - the method is rounding up
- for 2.5 - the method is rounding down.
It is because, in the case of .5, the method rounds to nearest even value. Hence, in both cases, the method rounds to 2.0.