Philipp Schneider
Philipp Schneider

Reputation: 356

Different Formats between java8 and java12

With Java8 the following code worked fine. Now i want to upgrade to java12 i've got the problem, that this will not work.

  public static void main(String[] args) {
    Double d = new Double(123456.8912);
    Locale locale = new Locale.Builder().setLanguage("de").setRegion("AT").build();

    DecimalFormat decimalFormat = (DecimalFormat) DecimalFormat.getInstance(locale);
    decimalFormat.applyLocalizedPattern("#.##0,00");

    System.out.println(decimalFormat.format(d.doubleValue()));
    // Excpected is 123.456,89
    // Output is 123456,89.
  }

Upvotes: 2

Views: 2495

Answers (2)

Philipp Schneider
Philipp Schneider

Reputation: 356

The problem is not a java one. Seems to be a bug in CLDR. Austria differ from Germany, but should not.

(http://openjdk.java.net/jeps/252) Java used data from CLDR.

With the parameter -Djava.locale.providers=COMPAT,SPI set it will act like java8.

I will open a ticket at CLDR for this problem.

Upvotes: 2

Arpan Kanthal
Arpan Kanthal

Reputation: 503

Not sure if this is a defect on the java end but the problem seems to be the Locale object. In java 8 , when you retrieve the GroupingSeparator (like decimalFormat.getDecimalFormatSymbols().getGroupingSeparator()) you will get a "." character. But in Java 12 the returned value is "á" which is unicode \u00E0. I tried changing the pattern to be "#\u00E0##0,00" but that didnt work. Then i changed the locale builder to use the UN M.49 3-digit code for Austria (which is 040. Complete list can be found here: https://unstats.un.org/unsd/methodology/m49/) . That solved the problem. Below is the code i used

public static void main(String[] args) {
    Double d = new Double(123456.8912);
    Locale locale = new Locale.Builder().setLanguage("de").setRegion("040").build();

    DecimalFormat decimalFormat = (DecimalFormat) DecimalFormat.getInstance(locale);
    System.out.println(decimalFormat.getDecimalFormatSymbols().getDecimalSeparator());
    System.out.println( decimalFormat.getDecimalFormatSymbols().getGroupingSeparator());
    System.out.println((int) decimalFormat.getDecimalFormatSymbols().getGroupingSeparator());
    decimalFormat.applyLocalizedPattern("#.##0,00");

    System.out.println(decimalFormat.format(d.doubleValue()));
    // Excpected is 123.456,89
    // Output is 123456,89.
}

Let me know if this atleast solves the problem , if not reveal what the problem is. Also found that the Locale specific digits and grouping separator is provided by the jre, so it might just be the underlying provider having a problem

Upvotes: 1

Related Questions