Java Currency and Locale - Updates

07 Apr 2023

This is an update to Java Currency and Locale - Miscellaneous Notes.

Older versions of Java (using older versions of CLDR rules) used to support code like this:

Java
1
Double amount = NumberFormat.getCurrencyInstance(Locale.GERMANY).parse("9,99 €");

This would parse the currency string to the number 9.99.

In Java 17 and later (and possibly earlier), this behavior has changed. The standard space character (U+0020) is no longer the expected separator between the number and the currency symbol. The non-breaking space (U+00A0) is now the expected separator.

So, the following works:

Java
1
Double amount = NumberFormat.getCurrencyInstance(Locale.GERMANY).parse("9,99\u00A0€");

Or this:

Java
1
2
Double amount = NumberFormat.getCurrencyInstance(Locale.GERMANY)
        .parse("9,99 €".replace(" ", "\u00A0"));

I suppose we need to be careful that the only space in the string is the one between the amount and the currency symbol…