Skip to content
Advertisement

Discrepancies between Java 11 and Java 8 DecimalFormat behaviour

I’ve got a weird issue with Java 11. I’m porting a huge project, and I have some tests which use DecimalFormat failing on the 11 build, but not on the 8 build.

DecimalFormat in Java 11 gives me a comma decimal separator, and on Java 8 it gives me a dot.

Here is a minimal reproducible example:

import java.text.DecimalFormat;

public class Test {

  public static void main(String[] args) {
    DecimalFormat format = new DecimalFormat("#0.00");
    System.out.println(format.format(1.02));
  }
}

which outputs 1.02 on Java 8, and 1,02 on Java 11.

Here is the entire terminal session of building and running the example with both versions:

~ » sdk use java 8.0.282.hs-adpt

Using java version 8.0.282.hs-adpt in this shell.

~ » javac -version 
javac 1.8.0_282

~ » javac Test.java                                   

~ » java -version                                                                                                                                                                                                       
openjdk version "1.8.0_282"
OpenJDK Runtime Environment (AdoptOpenJDK)(build 1.8.0_282-b08)
OpenJDK 64-Bit Server VM (AdoptOpenJDK)(build 25.282-b08, mixed mode)

~ » java Test                                                                                                                                                                                                           
1.02                                                                

~ » sdk use java 11.0.10.hs-adpt                                                                                                                                                                                        

Using java version 11.0.10.hs-adpt in this shell.

~ » javac -version                                                                                                                                                                                                      
javac 11.0.10

~ » javac Test.java                                             

~ » java -version                                                                                                                                                                                                       
openjdk version "11.0.10" 2021-01-19
OpenJDK Runtime Environment AdoptOpenJDK (build 11.0.10+9)
OpenJDK 64-Bit Server VM AdoptOpenJDK (build 11.0.10+9, mixed mode)

~ » java Test                                                                                                                                                                                                           
1,02

Has anyone had similar issues? Were there any changes to relevant classes between 8 and 11 which would cause this?

Advertisement

Answer

Yes, one of the changes was updating Locale Data to Unicode CLDR v33 (see https://www.oracle.com/java/technologies/javase/jdk-11-relnote.html)

This change is relevant for locale-specific formatting, which impacts the usage of the utils like DecimalFormat or SimpleDateFormat.

So if your code depends on locale-specific formatting, you should double check it after switching to Java 11.

User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement