9

I am using jdk 1.8.0_45, and our tests discovered a bug in rouding. RoundingMode.HALF_DOWN works the same as RoundingMode.HALF_UP when the last decimal that decide the rounding is 5.

I found related issues with RoundingMode.HALF_UP, but they are fixed in update 40. I also put a bug to the oracle, but from my experience they are really unresponsive.

package testjava8;

import java.math.RoundingMode;
import java.text.DecimalFormat;

public class Formatori {

    public static void main(String[] args) {
        DecimalFormat format = new DecimalFormat("#,##0.0000");
        format.setRoundingMode(RoundingMode.HALF_DOWN);
        Double toFormat = 10.55555;
        System.out.println("Round down");
        System.out.println(format.format(toFormat));

        format.setRoundingMode(RoundingMode.HALF_UP);
        toFormat = 10.55555;
        System.out.println("Round up");
        System.out.println(format.format(toFormat));
    }
}

Actual result: Round down 10.5556 Round up 10.5556

Expected result(obtain with jdk 1.7): Round down 10.5555 Round up 10.5556

Sergey Ponomarev
  • 2,947
  • 1
  • 33
  • 43
cristi
  • 361
  • 3
  • 9
  • 2
    That bug doesn't reproduce with jdk 1.8.0_45. And I read the issue that you said, but is not the same problem. That issue was fixed. – cristi Jun 11 '15 at 11:14

2 Answers2

10

Seems that it's intended change. The JDK 1.7 behavior was incorrect.

The problem is that you simply cannot represent the number 10.55555 using the double type. It stores the data in IEEE binary format, so when you assign the decimal 10.55555 number to the double variable, you actually get the closest value which can be represented in IEEE format: 10.555550000000000210320649784989655017852783203125. This number is bigger than 10.55555, so it's correctly rounded to 10.5556 in HALF_DOWN mode.

You can check some numbers which can be exactly represented in binary. For example, 10.15625 (which is 10 + 5/32, thus 1010.00101 in binary). This number is rounded to 10.1562 in HALF_DOWN mode and 10.1563 in HALF_UP mode.

If you want to restore the old behavior, you can first convert your number to BigDecimal using BigDecimal.valueOf constructor, which "translates a double into a BigDecimal, using the double's canonical string representation":

BigDecimal toFormat = BigDecimal.valueOf(10.55555);
System.out.println("Round down");
System.out.println(format.format(toFormat)); // 10.5555

format.setRoundingMode(RoundingMode.HALF_UP);
toFormat = BigDecimal.valueOf(10.55555);
System.out.println("Round up");
System.out.println(format.format(toFormat)); // 10.5556
Tagir Valeev
  • 97,161
  • 19
  • 222
  • 334
  • Note that Java allows underscore in number literals. Using them to mark the point for rounding may help readers through the bunch of `5`s… – Holger Jun 11 '15 at 12:05
  • 1
    I was about to post a similar answer since the behaviour seemed correct to me. This would be issue [JDK-7131459](https://bugs.openjdk.java.net/browse/JDK-7131459). However it seems it should have been [backported in 7u40](https://bugs.openjdk.java.net/browse/JDK-8000978) so why does it occur with 7u80? Or maybe this was a different bug? – Didier L Jun 11 '15 at 12:06
  • 1
    The DecimalFormat class does not say if it will work on the exact or the canonical representation of the double (unless I missed it) so both behaviours seem acceptable... – assylias Jun 11 '15 at 12:07
5

The change of behaviour is documented in the release notes of Java 8

When using the NumberFormat and DecimalFormat classes, the rounding behavior of previous versions of the JDK was wrong in some corner cases. [...]

As an example, when using the default recommended NumberFormatFormat API form: NumberFormat nf = java.text.NumberFormat.getInstance() followed by nf.format(0.8055d), the value 0.8055d is recorded in the computer as 0.80549999999999999378275106209912337362766265869140625 since this value cannot be represented exactly in the binary format. Here, the default rounding rule is "half-even", and the result of calling format() in JDK 7 is a wrong output of "0.806", while the correct result is "0.805", since the value recorded in memory by the computer is "below" the tie.

This new behavior is also implemented for all rounding positions that might be defined by any pattern chosen by the programmer (non default patterns).

Community
  • 1
  • 1
assylias
  • 321,522
  • 82
  • 660
  • 783