Java Math.rint(double x) method returns the double value that is nearest to the given argument x
and equal to a mathematical integer number. If two integer numbers are equally close to the given argument then this method returns the even number.
public class JavaExample { public static void main(String[] args) { double x = 32.5; double x2 = 31.5; //32 and 33 are equally close to x //even number 32 is chosen System.out.println(Math.rint(x)); //31 and 32 are equally close to x //even number 32 is chosen System.out.println(Math.rint(x2)); } }
Output:
32.0 32.0
Syntax of Math.rint() method
Math.rint(16.5); //returns 16.0
rint() Description
public static double rint(double x): Returns the closest double value to the argument x, this value is equal to a mathematical integer.
rint() Parameters
- x: A double value.
rint() Return value
- Closest floating point value to the argument x, which is equal to an integer.
- If the argument x is NaN (Not a number), infinity or zero, then it returns the same argument with the same sign.
Example 1
public class JavaExample { public static void main(String[] args) { double x = 17.5; double x2 = -17.5; System.out.println(Math.rint(x)); System.out.println(Math.rint(x2)); } }
Output:

Example 2
public class JavaExample { public static void main(String[] args) { double x = 0; double x2 = -5.0/0; //-ve infinity double x3 = Double.NaN; System.out.println(Math.rint(x)); System.out.println(Math.rint(x2)); System.out.println(Math.rint(x3)); } }
Output:

Example 3
public class JavaExample { public static void main(String[] args) { double x = Double.MAX_VALUE; double x2 = Double.MIN_VALUE; System.out.println(Math.rint(x)); System.out.println(Math.rint(x2)); } }
Output:
