abs  Difference between Math.rint() and Math.round() in Java
math.round in java (5)
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(4.501) = 5.0
Math.round(4.501) = 5
As you can see, Math.rint(4.5)
actually rounds down, 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/javaprogrammerOCPJP/certification/DifferencerintmethodsMathclass
What is the difference between Math.rint()
and Math.round()
?
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
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
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.
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
.
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 twodigit 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