Search code examples
javainternationalizationcurrencyjava-17cldr

Idiomatic way to remove country code from currency format?


Somewhere between Java 11 and 17 currency formatting changed to where this:

NumberFormat.getCurrencyInstance(Locale.CANADA_FRENCH).format(100.00)

would print 100,00 $ CA instead of 100,00 $.

Is there a better way than this to remove the country code CA?

var currencyFormat = NumberFormat.getCurrencyInstance(Locale.CANADA_FRENCH);
if (currencyFormat instanceof DecimalFormat decimalFormat) {
    var symbols = DecimalFormatSymbols.getInstance(Locale.CANADA_FRENCH);
    symbols.setCurrencySymbol("$");
    decimalFormat.setDecimalFormatSymbols(symbols);
}

Seems a bit much just to get back something that was the default behavior up until recently.


Solution

  • I dug a bit into this, the JDK locale data comes from Unicode CLDR by default, and it seems they reverted from $ CA to $ back in August, see CLDR-14862 and this commit (expand common/main/fr_CA.xml and then go to lines 5914/5923).

    This was part of v40, released in October, so too late for JDK 17 whose doc says it uses CLDR v35.1 (which was introduced in Java 13) but it seems it was updated to v39 in April 2021 and they forgot the release note (JDK 16 appears to have been upgraded to v38 already).

    CLDR v40 is planned for JDK 19.

    You may want to run your application using the COMPAT locales first, with

    -Djava.locale.providers=COMPAT,CLDR,SPI
    

    (found here but see also LocaleServiceProvider)

    This will use the locales compatible with Java 8, where this issue is not present.