What is the difference between Math.rint()
and Math.round()
?
-
1A rare instance of a useful Code Ranch post: http://www.coderanch.com/t/239803/java-programmer-OCPJP/certification/Difference-rint-methods-Math-class `Math.rint()` ande `Math.round()` behave differently for inputs exactly in between two integers. – Tim Biegeleisen May 16 '16 at 14:45
-
3@TimBiegeleisen: If you can add some meat to that, it should be an answer. – Makoto May 16 '16 at 14:48
7 Answers
Math.rint()
and Math.round()
have several differences, but the one which might impact the business logic of a Java application the most is the way they handle rounding of integers which are on a boundary (e.g. 4.5
is on the boundary of 4
and 5
). Consider the following code snippet and output:
double val1 = 4.2;
double val2 = 4.5;
System.out.println("Math.rint(" + val1 + ") = " + Math.rint(val1));
System.out.println("Math.round(" + val1 + ") = " + Math.round(val1));
System.out.println("Math.rint(" + val2 + ") = " + Math.rint(val2));
System.out.println("Math.round(" + val2 + ") = " + Math.round(val2));
System.out.println("Math.rint(" + (val2 + 0.001d) + ") = " + Math.rint(val2 + 0.001d));
System.out.println("Math.round(" + (val2 + 0.001d) + ") = " + Math.round(val2 + 0.001d));
Output:
Math.rint(4.2) = 4.0
Math.round(4.2) = 4
Math.rint(4.5) = 4.0
Math.round(4.5) = 5
Math.rint(5.5) = 6.0
Math.round(5.5) = 6
Math.rint(4.501) = 5.0
Math.round(4.501) = 5
As you can see, Math.rint(4.5)
actually converts to the nearest even integer, while Math.round(4.5)
rounds up, and this deserves to be pointed out. However, in all other cases, they both exhibit the same rounding rules which we would expect.
Here is a useful Code Ranch article which briefly compares Math.rint()
and Math.round()
: http://www.coderanch.com/t/239803/java-programmer-OCPJP/certification/Difference-rint-methods-Math-class

- 637
- 4
- 24

- 502,043
- 27
- 286
- 360
-
11An important note here is that .rint() doesn't universally round down. It actually rounds to the nearest EVEN integer on a .5 boundary. If you did Math.rint(1.5), it actually rounds up to 2.0. – Fleep Dec 27 '16 at 14:52
-
@Fleep Is there any mathematical point of rounding to an *even* number? – Matthieu Mar 15 '17 at 10:11
-
2
-
3Thanks @Fleep. To summarize: *This method treats positive and negative values symmetrically, and is therefore free of sign bias*. – Matthieu Mar 16 '17 at 16:25
-
Conclusion: Both `round` and `rint` round to closest integer. When the argument is halfway between two integers, the former rounds up (to positive infinity) while the latter rounds to even integer. Note: With `rint` by rounding to even, we can avoid statistical bias when we calculate an average of set of numbers. – rosshjb Jan 22 '22 at 15:58
Example of Math.rint(): 2.50 lies between 2.00 and 3.00. Math.rint() returns the closest even double value. Math.rint(2.50) returns 2.0.
Example of Math.round(): 2.50 lies between 2.00 and 3.00. Math.round() returns the closest higher whole number. Math.round(2.50) returns 3

- 385
- 8
- 21
They behave differently when passed something ending in .5
, signed 0, NaN, or an Infinity.
Math.round
accepts both double
s and float
s, and has varying return types for some reason (long
and int
respectively, which are the smallest type large enough to cover the entire range represented by the parameter).
Math.rint
accepts double
s and returns double
s. It is less "destructive" than Math.round
because it doesn't alter values under several conditions (see below).
The behavior of this method follows IEEE Standard 754, section 4. This kind of rounding is sometimes called rounding to nearest, or banker's rounding. It minimizes rounding errors that result from consistently rounding a midpoint value in a single direction.
(From Jon Skeet's answer in C#. The behavior of Math.Round in C# is more similar to Java's Math.rint
, as confusing as that may be.)
From the docs:
static double rint(double a)
Returns the double value that is closest in value to the argument and is equal to a mathematical integer.
Returns the double value that is closest in value to the argument and is equal to a mathematical integer. If two double values that are mathematical integers are equally close, the result is the integer value that is even.
Special cases:
If the argument value is already equal to a mathematical integer, then the result is the same as the argument.
If the argument is NaN or an infinity or positive zero or negative zero, then the result is the same as the argument.
...
static long round(double a)
...
static int round(float a)
Returns the closest int to the argument, with ties rounding up.
Special cases:
- 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, the result is equal to the value of Integer.MIN_VALUE.
- If the argument is positive infinity or any value greater than or equal to the value of Integer.MAX_VALUE, the result is equal to the value of Integer.MAX_VALUE.
You can also change the behavior of Math.round
via RoundingMode:
public enum RoundingMode extends Enum<RoundingMode>
Specifies a rounding behavior for numerical operations capable of discarding precision. Each rounding mode indicates how the least significant returned digit of a rounded result is to be calculated. If fewer digits are returned than the digits needed to represent the exact numerical result, the discarded digits will be referred to as the discarded fraction regardless the digits' contribution to the value of the number. In other words, considered as a numerical value, the discarded fraction could have an absolute value greater than one.
Each rounding mode description includes a table listing how different two-digit decimal values would round to a one digit decimal value under the rounding mode in question. The result column in the tables could be gotten by creating a BigDecimal number with the specified value, forming a MathContext object with the proper settings (precision set to 1, and the roundingMode set to the rounding mode in question), and calling round on this number with the proper MathContext. A summary table showing the results of these rounding operations for all rounding modes appears below.
Result of rounding input to one digit with the given rounding
Input UP DOWN CEILING FLOOR HALF_UP HALF_DOWN HALF_EVEN UNNECESSARY 5.5 6 5 6 5 6 5 6 throw ArithmeticException 2.5 3 2 3 2 3 2 2 throw ArithmeticException 1.6 2 1 2 1 2 2 2 throw ArithmeticException 1.1 2 1 2 1 1 1 1 throw ArithmeticException 1.0 1 1 1 1 1 1 1 1 -1.0 -1 -1 -1 -1 -1 -1 -1 -1 -1.1 -2 -1 -1 -2 -1 -1 -1 throw ArithmeticException -1.6 -2 -1 -1 -2 -2 -2 -2 throw ArithmeticException -2.5 -3 -2 -2 -3 -3 -2 -2 throw ArithmeticException -5.5 -6 -5 -5 -6 -6 -5 -6 throw ArithmeticException

- 5,965
- 14
- 31
- 57
There is a type return difference:
Math.round()
returns long
or int
Math.rint()
returns double
But the crucial difference is in the numerical treatment of values at 0.5
.
Math.round()
converts 9.5 <= x < 10.5
to 10
Math.rint()
converts 9.5 <= x <= 10.5
to 10.0
Math.round()
converts 10.5 <= x < 11.5
to 11
Math.rint()
converts 10.5 < x < 11.5
to 11.0
Math.round()
converts 11.5 <= x < 12.5
to 12
Math.rint()
converts 11.5 <= x <= 12.5
to 12.0
(Note the inequalities!) So Math.round()
always rounds up at the mid-point (0.5): documentation.
Instead, Math.rint()
favours the closest even number at the mid-point: documentation.
For example, try running the following trivial example:
public class HelloWorld{
public static void main(String []args){
System.out.println("Math.round() of 9.5 is " + Math.round(9.5));
System.out.println("Math.round() of 10.5 is " + Math.round(10.5));
System.out.println("Math.round() of 11.5 is " + Math.round(11.5));
System.out.println("Math.round() of 12.5 is " + Math.round(12.5));
System.out.println("Math.rint() of 9.5 is " + Math.rint(9.5));
System.out.println("Math.rint() of 10.5 is " + Math.rint(10.5));
System.out.println("Math.rint() of 11.5 is " + Math.rint(11.5));
System.out.println("Math.rint() of 12.5 is " + Math.rint(12.5));
}
}
Note that the current top answer is wrong. I tried proposing an edit to his post but it was rejected. So as per the rejection comments, I'm putting my edits as a new answer.

- 41
- 1
- 5
-
This logic holds true for the negative numbers as well, in a nutshell, the major difference lies at the .5 decimal part. If the number is of type x.5 then rint() prefers an even double value. Thanks a lot Beast – Shubham Arya Sep 03 '20 at 03:18
Difference is at .5.
Math.round()
converts [10.5, 11.5[
to 11
Math.rint()
converts ]10.5, 11.5[
to 11.0
Math.round()
returns long or int.
Math.rint()
returns double.
So it can be said that Math.round()
favors mid-point(0.5) for higher value.

- 3
- 3

- 13,038
- 6
- 64
- 79
For positive Numbers:
if fractional part lies in 0 (inclusive) and 0.5 (exclusive) then round() give the integer part of the number.
if fractional part lies in 0.5 (inclusive) and 1 (exclusive) then round() give the integer part of the number + 1.
But in case of rint,
if fractional part lies in 0 (inclusive) and 0.5 (inclusive) then rint() give the integer part of the number.
if fractional part lies in 0.5 (exclusive) and 1 (exclusive) then round() give the integer part of the number + 1.
For negative number:
Both has same behavior, i.e
if fractional part lies in 0 (inclusive) and 0.5 (inclusive), integer part of the number is returned.
if fractional part lies in 0.5 (inclusive) and 1 (exclusive) integer part of the number -1.
fractional part is just the part of number of after decimal

- 325
- 6
- 12
Owing to the fact that your question has been heavily answered, I won't run over the obvious points but will provide a helpful article I came across:
What I found of utmost importance is the fact that round returns the closest higher whole number as an integer or long.

- 161
- 3
- 8