Java HALF_EVEN 四舍五入是什么?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28134938/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 05:39:32  来源:igfitidea点击:

What is HALF_EVEN rounding for?

javarounding

提问by Xin

I can not imagine a situation that I need to use RoundingMode.HALF_EVENin Java.

我无法想象我需要RoundingMode.HALF_EVEN在 Java 中使用的情况。

What is this rounding mode for? When do I want to use it?

这个四舍五入模式有什么用?我想在什么时候使用它?

Please give me some real world examples.

请给我一些现实世界的例子。

采纳答案by CorayThan

It is useful when you are performing multiple rounding operations and want the cumulative result to be a true average, and not skewed up or down, as it would be with HALF_UP or HALF_DOWN.

当您执行多个舍入操作并希望累积结果为真实平均值,而不是像 HALF_UP 或 HALF_DOWN 那样向上或向下倾斜时,它很有用。

Specifically, it is useful for statistical analysis (you don't want the results polluted by a non-random averaging system) or any situation where you want random averaging.

具体来说,它对于统计分析(您不希望结果被非随机平均系统污染)或任何您想要随机平均的情况都很有用。

回答by maja

RoundingMode.HALF_EVENalways rounds to the next number, like any other rounding-algorithmn - with only one execption: If the number-to-round is exacly between 2 numbers (2.5, 42.5, -4.5), it will not round it up, but instead round it to the neighbour which is even. Here are some examples:

RoundingMode.HALF_EVEN总是舍入到下一个数字,就像任何其他舍入算法一样 - 只有一个 execption:如果要舍入的数字恰好在 2 个数字(2.5、42.5、-4.5)之间,则不会将其舍入,而是舍入它是偶数的邻居。这里有些例子:

  • 3.2 -> 3
  • 3.4 -> 3
  • 3.5 -> 4
  • 4.5 -> 4
  • 5.5 -> 6
  • -7.5 -> -8
  • 3.2 -> 3
  • 3.4 -> 3
  • 3.5 -> 4
  • 4.5 -> 4
  • 5.5 -> 6
  • -7.5 -> -8

回答by Ronald Duck

The behavior is well described in the Javadoc:

该行为在Javadoc 中有很好的描述:

Rounding mode to round towards the "nearest neighbor" unless both neighbors are equidistant, in which case, round towards the even neighbor.

舍入模式向“最近邻居”舍入,除非两个邻居等距,在这种情况下,向偶数邻居舍入

So given the number 4.5, which falls right in the middle of the range of numbers between 4 and 5, when you call:

因此,给定数字 4.5,它正好位于 4 和 5 之间的数字范围的中间,当您调用时:

BigDecimal value1 = new BigDecimal("4.5").setScale(0, RoundingMode.HALF_EVEN);

The runtime needs to figure out which neighbor to round too, aka, should it round to 4, or to 5? Normally it would round based on which value 4.5 is closer to, but in this case its close to bothneighbors. Instead of arbitrarily picking the final result though, it picks the even number. This is the behavior of ROUND_HALF_EVEN. If you wanted to, you could specify ROUND_HALF_UPand the final result would have been 5, and not 4. Also, keep in mind that the determination about how to round is based on what the final result would be (and not on the decimal portion of the big decimal, as you seem to have assumed).

运行时还需要确定要舍入哪个邻居,也就是应该舍入到 4 还是 5?通常它会根据 4.5 更接近哪个值来舍入,但在这种情况下,它与两个邻居接近。不过,它不是随意选择最终结果,而是选择偶数。这是 的行为ROUND_HALF_EVEN。如果你愿意,你可以指定ROUND_HALF_UP最终结果是 5,而不是 4。另外,请记住,关于如何舍入的决定是基于最终结果是什么(而不是小数部分)大十进制,正如您似乎已经假设的那样)。

回答by SMA

Referring thissays:

提到这个说:

Rounding mode to round towards the "nearest neighbor" unless both neighbors are equidistant, in which case, round towards the even neighbor. Behaves as for RoundingMode.HALF_UP if the digit to the left of the discarded fraction is odd; behaves as for RoundingMode.HALF_DOWN if it's even. Note that this is the rounding mode that statistically minimizes cumulative error when applied repeatedly over a sequence of calculations. It is sometimes known as "Banker's rounding," and is chiefly used in the USA. This rounding mode is analogous to the rounding policy used for float and double arithmetic in Java.

Example:

Input ->    rounded  
5.5   ->    6  
2.5   ->    2  
1.6   ->    2  
1.1   ->    1  
1.0   ->    1  
-1.0  ->    -1  
-1.1  ->    -1  
-1.6  ->    -2  
-2.5  ->    -2  
-5.5  ->    -6   

So it rounds towards nearest value and if both are equidistant then it rounds towards an even number.

所以它向最接近的值四舍五入,如果两者等距,则向偶数四舍五入。

回答by Peter Lawrey

If you have random negative and positive numbers HALF_UP is fine and the net error will tend to 0. HALF_UP is also easier for a human to understand and is often used in finance.

如果你有随机的负数和正数 HALF_UP 很好,净误差将趋于 0。 HALF_UP 也更容易被人类理解,经常用于金融。

However, if you know you have more of positive (or negative) numbers you will get a bias. HALF_EVEN and HALF_ODD attempts to correct for this by choosing whether to rounding 0.5 up or down based on whether it is more likely to go to an even or odd number. This is statistically fairer, provided you have a 50/50 split of even and odd numbers, however harder for a human to understand.

然而,如果你知道你有更多的正(或负)数,你就会有偏差。HALF_EVEN 和 HALF_ODD 试图通过选择是向上还是向下舍入 0.5 来纠正这个问题,这取决于它更有可能变成偶数还是奇数。这在统计上更公平,前提是您有 50/50 的偶数和奇数分割,但人类更难理解。