5

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?

  • 4
    Looks like a different `Locale` is used. – Henry Feb 23 '21 at 14:41
  • Maybe you're having a problem with the regional settings? Take a look at this question: https://stackoverflow.com/questions/5054132/how-to-change-the-decimal-separator-of-decimalformat-from-comma-to-dot-point – raven1981 Feb 23 '21 at 14:41
  • 1
    [this answer](https://stackoverflow.com/questions/65218374/how-to-use-unsupported-locale-in-java-11-and-numbers-in-string-format/65221042#65221042) and [this thread](https://stackoverflow.com/questions/46244724/jdk-dateformatter-parsing-dayofweek-in-german-locale-java8-vs-java9) might be helpful – Naman Feb 23 '21 at 16:29

1 Answers1

7

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.

9ilsdx 9rvj 0lo
  • 7,955
  • 10
  • 38
  • 77
  • I still don't see why this happens. The locale that is used is not changed, is it? Why does the decimal separator of the same locale change between Java versions? – gsl Feb 23 '21 at 18:59
  • @gsl because they've changed locale data (which includes decimal separators) – 9ilsdx 9rvj 0lo Feb 24 '21 at 07:39