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.
.replace(" CA", "")
to the end? btw, this behaviour is not in java 15. – Senghorpublic static final NumberFormat CANADA_FRENCH_FORMAT = customCanadaFormat();
and put your code in thestatic customCanadaFormat()
method. If you really want to "undo" this behaviour change, you could use reflection to modifyLocale.CANADA_FRENCH
. It almost feels like a bug TBH. Generally, java is backwardly compatible. This is a (minor) breaking change. – SenghorLocale.setDefaultLocale
). – BursonDecimalFormatSymbols.getInstance(Locale.CANADA_FRENCH)
you can usedecimalFormat.getDecimalFormatSymbols()
. But that’s the only potential simplification I see. – Gentianaceous