In Java, Math.rint(a) returns the whole number closest to a, as a double. When a is exactly halfway between two whole numbers, it picks the even one: Math.rint(2.5) is 2.0, but Math.rint(3.5) is 4.0.
This rule is called round half to even or banker's rounding. Because half of the ties go up and half go down, totals of many rounded values stay closer to the real total.
A static method of the java.lang.Math class, available since the early versions of Java (JDK 1.0). Call it on the class name, as in Math.rint(...). No import is needed, because classes in java.lang are always visible.
Syntax
Pass a double; the nearest whole number comes back as a double.
Java
double a = Math.rint(2.4); // 2.0
double b = Math.rint(2.5); // 2.0 (tie: 2 is even)
double c = Math.rint(3.5); // 4.0 (tie: 4 is even)
double d = Math.rint(-2.6); // -3.0
Call
What it does
Math.rint(7.5)
Tie: returns 8.0.
Math.rint(6.5)
Tie: returns 6.0.
Math.rint(x * 10) / 10
Round to one decimal, ties to even.
(long) Math.rint(x)
Nearest whole number as a long.
The name comes from C's rint ("round to integer"). Unlike Math.round, it never converts the result to an integer type, so it also works for values larger than any long.
Library declarations
Java
// java.lang.Math
public static double rint(double a)
Parameters
Parameter
Description
a
The value to round.
Return Value
Return Value
Description
double
The whole number nearest to a; for an exact .5 tie, the even one. NaN, infinities, zeros, and whole values come back unchanged.
Round always pushes ties up.Math.round turns 0.5, 1.5, 2.5, and 3.5 into 1, 2, 3, and 4, adding 0.5 every time, so the total grows to 10.
rint balances them. It gives 0, 2, 2, and 4: two ties go down and two go up, and the errors cancel out to exactly 8.0.
Different result types. The rint total is a double (8.0), while the round total is a long (10), because the two methods return different types.
Example 3 — Round to One Decimal Place With Ties to Even
Scale by 10, round, and scale back. These inputs are exact in binary, so each one is a true tie.
Java
public class MathRintOneDecimal {
public static void main(String[] args) {
double[] grades = {0.25, 0.75, 1.25};
for (double g : grades) {
System.out.println(g + " -> " + Math.rint(g * 10) / 10);
}
System.out.println("NaN -> " + Math.rint(Double.NaN));
}
}
Output
0.25 -> 0.2
0.75 -> 0.8
1.25 -> 1.2
NaN -> NaN
How It Works
Exact ties on purpose. 0.25, 0.75, and 1.25 are sums of powers of two, so multiplying by 10 gives exactly 2.5, 7.5, and 12.5.
Even neighbour, then shift back.rint turns them into 2.0, 8.0, and 12.0, and dividing by 10 prints 0.2, 0.8, and 1.2.
NaN in, NaN out. There is no nearest whole number to "not a number", so rint returns NaN unchanged.
Frequently Asked Questions
It rounds a double to the nearest whole number and returns a double. If the value is exactly halfway, it chooses the even whole number, so Math.rint(2.5) is 2.0.
rint sends ties to the even neighbour and returns a double. round sends ties toward positive infinity and returns a long (or an int for a float). Math.rint(2.5) is 2.0 while Math.round(2.5) is 3.
Another name for round half to even: exact halves are rounded to the nearest even number. Over many values, half the ties go up and half go down, so totals are not pushed upward.
Because 0.545 is stored slightly above 0.545, so the product is 54.50000000000001, not an exact tie, and the nearest whole number is 55.0. Only exact halves go to the even neighbour. Use BigDecimal with RoundingMode.HALF_EVEN for exact decimal rounding.
It returns -0.0. The nearest whole number is zero, and rint keeps the sign of the input.