Java Math rint() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
java.lang.Math

Definition

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
CallWhat it does
Math.rint(7.5)Tie: returns 8.0.
Math.rint(6.5)Tie: returns 6.0.
Math.rint(x * 10) / 10Round 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

ParameterDescription
aThe value to round.

Return Value

Return ValueDescription
doubleThe whole number nearest to a; for an exact .5 tie, the even one. NaN, infinities, zeros, and whole values come back unchanged.

Compare with Similar Methods

How rint differs from the other rounding methods:

MethodHow it differs
round(a)Ties go toward positive infinity (2.5 to 3); returns long.
floor(a)Always down; returns double.
ceil(a)Always up; returns double.
RoundingMode.HALF_EVENThe same tie rule for BigDecimal, on exact decimals.

Java Examples

Example 1 — Nearest Whole Number, With Ties to Even

Round a mix of ordinary values and exact halves.

Java
public class MathRintNearest {
    public static void main(String[] args) {
        double[] values = {2.4, 2.6, 2.5, 3.5, -2.6, -0.4};
        for (double v : values) {
            System.out.println("rint(" + v + ") = " + Math.rint(v));
        }
    }
}

How It Works

  1. Ordinary values. 2.4 is closer to 2 and 2.6 is closer to 3, so rint behaves like normal rounding.
  2. Exact ties. 2.5 and 3.5 sit exactly halfway; rint picks the even neighbours 2.0 and 4.0, so one tie goes down and the other up.
  3. Negatives keep their sign. -2.6 rounds to -3.0, and -0.4 rounds to zero but keeps the minus sign, printing -0.0.

Example 2 — Why Banks Like Ties to Even

Add up rounded amounts that all end in .5 and compare the totals.

Java
public class MathRintBias {
    public static void main(String[] args) {
        double[] amounts = {0.5, 1.5, 2.5, 3.5};
        double exact = 0;
        double withRint = 0;
        long withRound = 0;
        for (double a : amounts) {
            exact += a;
            withRint += Math.rint(a);
            withRound += Math.round(a);
        }
        System.out.println("Exact total: " + exact);
        System.out.println("rint total:  " + withRint);
        System.out.println("round total: " + withRound);
    }
}

How It Works

  1. 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.
  2. 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.
  3. 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));
    }
}

How It Works

  1. 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.
  2. 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.
  3. 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.

Next: round()

Round to the nearest int or long; halves round up, toward positive infinity.

round() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

14 people found this page helpful