What is HALF_EVEN rounding for? [closed]

泄露秘密 提交于 2019-12-21 06:51:34

问题


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

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

Please give me some real world examples.


回答1:


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.

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.




回答2:


RoundingMode.HALF_EVEN always 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:

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



回答3:


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.

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.




回答4:


The behavior is well described in the 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:

BigDecimal value1 = new BigDecimal("4.5").setScale(RoundingMode.ROUND_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 both neighbors. 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_UP and 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).




回答5:


Referring this says:

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.



来源:https://stackoverflow.com/questions/28134938/what-is-half-even-rounding-for

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!