I've found what I believe is a DecimalFormat regression bug in Java 8, i.e. 
Java 8 seems to format some numbers differently than previous releases do.

I've tried submitting a proper bug report using the form at bugreport.sun.com, 
but it just throws back a "submission error" at me, so I resort to mailing you.

Just try the code below.
--------------
import java.text.DecimalFormat;
public class Jdk8FormattingBug {

   public static void main(String[] args) {
       double dd[] = {-0.15, -0.05, 0.05, 0.15, 0.25, 0.35, 0.45, 0.55, 0.65};
    DecimalFormat fmt = new DecimalFormat("0.#");

    for (double d : dd) {
    System.out.println(d+" "+fmt.format(d));
    }
    }
}
----------------

I've tested JDK 1.8 b108 and b111, on Linux and on Mac, and they all print:

-0.15 –0.1
-0.05 –0.1
0.05 0.1
0.15 0.1
0.25 0.2
0.35 0.3
0.45 0.5
0.55 0.6
0.65 0.7


Yet the printout using all other Java versions I've tried is:

-0.15 –0.2
-0.05 -0
0.05 0
0.15 0.2
0.25 0.2
0.35 0.4
0.45 0.4
0.55 0.6
0.65 0.6

which is the documented behaviour, respecting RoundingMode.HALF_EVEN.

I'd expect formatting to behave the same in Java 8, or have I missed some 
change here? I've noticed there has been some optimisation work done on 
DecimalFormat, but shouldn't the output stay the same?

Best regards,

Lennart Börjeson

Reply via email to