DecimalFormat.applyPattern("#.##0,00") throwing an exception

David Thielen picture David Thielen · Feb 24, 2017 · Viewed 7.7k times · Source

For the code:

DecimalFormat df = (DecimalFormat) NumberFormat.getNumberInstance(locale);
df.applyPattern("#.##0,00");

And it throws an IllegalArgumentException with a message of Malformed pattern "#.##0,00"

What is wrong with this?

Edit: I want to use the . as a thousands separator and , as the decimal. I know that's ass-backwards but have a case where that's the format they want.

Answer

john16384 picture john16384 · Feb 25, 2017

You need to use the pattern "#,##0.##" to indicate where you want to decimal separator and the thousand seperator. The Locale you use when using this DecimalFormat will then determine if the decimal separator is a . or , -- the pattern should NOT be changed.

If you are not getting the correct separators, you will need to use a different Locale.