Java 中 Math.rint() 和 Math.round() 的区别
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/37256550/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Difference between Math.rint() and Math.round() in Java
提问by
What is the difference between Math.rint()
and Math.round()
?
Math.rint()
和 和有Math.round()
什么区别?
回答by Laurel
They behave differently when passed something ending in .5
, signed 0, NaN, or an Infinity.
当传递以.5
、带符号 0、NaN 或无穷大结尾的内容时,它们的行为会有所不同。
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.round
同时接受double
S和float
S,并且具有变化的返回类型出于某种原因(long
以及int
分别,这是最小的类型大到足以覆盖由参数所表示的整个范围)。
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).
Math.rint
接受double
s 并返回double
s。它的“破坏性”较小,Math.round
因为它不会在多种条件下改变值(见下文)。
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.
此方法的行为遵循 IEEE 标准 754 第 4 节。这种舍入有时称为最接近的舍入或银行家舍入。它最大限度地减少了由于在单个方向上对中点值进行一致舍入而导致的舍入误差。
(From Jon Skeet's answerin C#. The behavior of Math.Round in C# is more similar to Java's Math.rint
, as confusing as that may be.)
(来自Jon Skeet在 C# 中的回答。C# 中 Math.Round 的行为更类似于 Java 的Math.rint
,尽管这可能会令人困惑。)
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 double rint(double a)
返回值与参数最接近且等于数学整数的双精度值。
返回值与参数最接近且等于数学整数的双精度值。如果作为数学整数的两个 double 值同样接近,则结果是偶数的整数值。
特别案例:
如果参数值已经等于一个数学整数,则结果与参数相同。
如果参数是 NaN 或无穷大或正零或负零,则结果与参数相同。
...
...
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.
static long round(double a)
...
static int round(float a)
返回最接近参数的 int,并向上舍入关系。
特别案例:
- 如果参数为 NaN,则结果为 0。
- 如果参数为负无穷大或任何小于或等于 Integer.MIN_VALUE 的值,则结果等于 Integer.MIN_VALUE 的值。
- 如果参数为正无穷大或任何大于或等于 Integer.MAX_VALUE 的值,则结果等于 Integer.MAX_VALUE 的值。
You can also change the behavior of Math.round
.
您还可以更改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 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.
public enum RoundingMode extends Enum<RoundingMode>
指定能够舍弃精度的数值运算的舍入行为。每个舍入模式指示如何计算舍入结果的最低有效返回数字。如果返回的数字少于表示精确数字结果所需的数字,则丢弃的数字将被称为丢弃的分数,而不管这些数字对数字值的贡献。换句话说,被视为一个数值,丢弃的分数可以具有大于一的绝对值。
每个舍入模式描述都包含一个表格,列出不同的两位十进制值在所讨论的舍入模式下如何舍入为一位十进制值。表中的结果列可以通过创建具有指定值的 BigDecimal 数,形成具有适当设置的 MathContext 对象(精度设置为 1,并将 roundingMode 设置为所讨论的舍入模式),并对此调用 round 来获得具有适当 MathContext 的数字。下面显示了一个汇总表,显示了所有舍入模式的这些舍入操作的结果。
| 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
回答by Tim Biegeleisen
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:
Math.rint()
而Math.round()
有一些差异,但其中一个可能会影响到Java应用程序的业务逻辑的最多的就是他们处理它们的边界上整数四舍五入的方式(例如4.5
是在边界上4
和5
)。考虑以下代码片段和输出:
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.
如您所见,Math.rint(4.5)
实际上是向下舍入,而Math.round(4.5)
向上舍入,这一点值得指出。然而,在所有其他情况下,它们都表现出与我们期望的相同的舍入规则。
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
这是一篇有用的 Code Ranch 文章,它简要比较了Math.rint()
和Math.round()
:http://www.coderanch.com/t/239803/java-programmer-OCPJP/certification/Difference-rint-methods-Math-class
回答by Saurav Sahu
Difference is at .5.
差异为0.5。
Math.round()
converts [10.5, 11.5[
to 11 Math.rint()
converts ]10.5, 11.5[
to 11.0
Math.round()
转换[10.5, 11.5[
为 11 Math.rint()
转换 ]10.5, 11.5[
为 11.0
Math.round()
returns long or int.Math.rint()
returns double.
Math.round()
返回 long 或 int。Math.rint()
返回双倍。
So it can be said that Math.round()
favors mid-point(0.5) for higher value.
所以可以说Math.round()
偏向于中点(0.5)以获得更高的价值。
回答by User
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.
Math.rint() 示例:2.50 介于 2.00 和 3.00 之间。Math.rint() 返回最接近的偶数双精度值。Math.rint(2.50) 返回 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
Math.round() 示例:2.50 介于 2.00 和 3.00 之间。Math.round() 返回最接近的较高整数。Math.round(2.50) 返回 3
回答by Shiv Krishna Jaiswal
For positive Numbers:
对于正数:
if fractional part lies in 0 (inclusive) and 0.5 (exclusive) then round() give the integer part of the number.
如果小数部分位于 0(包含)和 0.5(不包含)内,则 round() 给出数字的整数部分。
if fractional part lies in 0.5 (inclusive) and 1 (exclusive) then round() give the integer part of the number + 1.
如果小数部分位于 0.5(包含)和 1(不包含)内,则 round() 给出数字的整数部分 + 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.
如果小数部分位于 0(含)和 0.5(含)内,则 rint() 给出数字的整数部分。
if fractional part lies in 0.5 (exclusive) and 1 (exclusive) then round() give the integer part of the number + 1.
如果小数部分位于 0.5(不包括)和 1(不包括)内,则 round() 给出数字的整数部分 + 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.
如果小数部分位于 0(含)和 0.5(含)之间,则返回数字的整数部分。
if fractional part lies in 0.5 (inclusive) and 1 (exclusive) integer part of the number -1.
如果小数部分位于数字 -1 的 0.5(含)和 1(不含)整数部分。
fractional part is just the part of number of after decimal
小数部分只是小数点后的部分
回答by Beast257
There is a type return difference:
有一个类型返回差异:
Math.round()
returns long
or int
Math.rint()
returns double
Math.round()
退货long
或int
Math.rint()
退货double
But the crucial difference is in the numerical treatment of values at 0.5
.
但关键的区别在于 处的值的数值处理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()
转换9.5 <= x < 10.5
为10
Math.rint()
转换 9.5 <= x <= 10.5
为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()
转换10.5 <= x < 11.5
为11
Math.rint()
转换 10.5 < x < 11.5
为11.0
Math.round()
converts 11.5 <= x < 12.5
to 12
Math.rint()
converts 11.5 <= x <= 12.5
to 12.0
Math.round()
转换11.5 <= x < 12.5
为12
Math.rint()
转换 11.5 <= x <= 12.5
为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.
(注意不等式!)所以Math.round()
总是在中点(0.5)四舍五入:文档。
相反,Math.rint()
倾向于在中间点最接近的偶数:文档。
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 answeris 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.
回答by Dhruv Erry
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 numberas an integeror long.
我发现最重要的是 round 返回最接近的更高整数作为integer或long的事实。