Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is HALF_EVEN rounding for? [closed]

Tags:

java

rounding

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.

like image 757
Xin Avatar asked Jan 25 '15 09:01

Xin


People also ask

What is RoundingMode Half_even?

HALF_EVEN. public static final RoundingMode HALF_EVEN. 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.

What is half even?

Half even rounding is a rounding process that rounds half way values to the nearest even numbers. Round half to even is a tie-breaking rule that is even less biased. If the fraction y is 0.5, then q will be the nearest even integer.

What is rounding mode in Bigdecimals Java?

The enum RoundingMode provides eight rounding modes: CEILING – rounds towards positive infinity. FLOOR – rounds towards negative infinity. UP – rounds away from zero. DOWN – rounds towards zero.

What is rounding mode floor?

When you round toward floor, both positive and negative numbers are rounded to negative infinity. As a result, a negative cumulative bias is introduced in the number.


2 Answers

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
like image 177
maja Avatar answered Oct 09 '22 01:10

maja


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.

like image 37
CorayThan Avatar answered Oct 09 '22 02:10

CorayThan