Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Half-Even Rounding

Tags:

java

rounding

To my understanding, with half-even rounding, if the number that you are rounding on is equidistant between its neighbors (for example 4.5 if you are rounding on one after the decimal), you round the preceding number to the nearest even number.

For example, if you are rounding to 0 digits after the decimal 1.5, 3.5, 5.5, 7.5, and 9.5 will all be rounded up to 2, 4, 6, 8, and 10 respectively. This all works well and fine in netbeans, but I am running into problems when I round to 1 place after the decimal.

package foo;
import java.text.NumberFormat;

public class Foo {
    public static void main(String[] args) {
        NumberFormat formatter = NumberFormat.getNumberInstance();     
        formatter.setMaximumFractionDigits(1);
        System.out.print(formatter.format(4.45));
    }
}

run:

4.5

BUILD SUCCESSFUL (total time: 0 seconds)

I would think that this would round to 4.4

Additionally, if I use 4.55 for input into format, 4.5 is output where I expect 4.6 Is there an error with java or with my understanding?

like image 355
Matthew Papageorge Avatar asked Jan 10 '15 02:01

Matthew Papageorge


People also ask

What is half even rounding?

What is Rounding Half Even Numbers? 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.

Is half rounded up or down?

That is, half-way values of x are always rounded up. For example, 23.5 gets rounded to 24, and −23.5 gets rounded to −23.

Is 0.5 rounded up or down?

However, it can be converted to a whole number by rounding it off to the nearest whole number. 0.5 rounded off to the nearest whole number is 1. Since, the value after decimal is equal to 5, then the number is rounded up to the next whole number. Hence, the whole number of 0.5 will be 1.

How do you round a half number?

When rounding to the nearest half, round the fraction to whichever half the fraction is closest to on the number line. If a fraction is equally close to two different halves, round the fraction up.


1 Answers

I'm not certain this is actually technically a bug.

The issue is that when you write 4.45 and the value is interpreted as a double, that is rounded to a binary fraction: a sum of multiples of powers of 2, to some precision. (That rounding itself is done with HALF_EVEN rounding, but with binary digits instead of decimal digits.)

That rounded value may be slightly above or slightly below 4.45, but NumberFormat will round the true binary value stored in the double, not the decimal value you wrote.

You can determine the true binary value you're actually getting by writing System.out.println(new BigDecimal(4.45)); When I do that, I get 4.45000000000000017763568394002504646778106689453125, which is the actual value being rounded to the nearest 0.1 -- which is definitely going to be 4.5.

like image 145
Louis Wasserman Avatar answered Sep 20 '22 15:09

Louis Wasserman