DecimalFormat "Multiple decimal separators in pattern"

Steffzilla picture Steffzilla · Apr 11, 2013 · Viewed 7.5k times · Source

I want to format decimal values as a currency value: 1234.56d should become "1.234,56" (this format is used in some European countries). I'm using the following pattern to format decimal values:

final DecimalFormat df = new DecimalFormat("###.###.###,00");
final String formatted = df.format(1234.56d);
System.out.println(formatted);

In fact I'm using SuperCSV's class FmtNumber, but that doesn't matter, because the pattern syntax is the same: new FmtNumber("###.###.###,00");

What I get is an IllegalArgumentException with the message "Multiple decimal separators in pattern".

I understand that there can be only one decimal separator. But in my case it should be the sign ",". And "," appears only one time in my pattern.

I searched the Web and StackOverflow for this exception message, but I found no helpful answers.

What's my error?

Answer

Boris the Spider picture Boris the Spider · Apr 11, 2013

From the javadoc in a format string . is always the decimal separator and , is always a grouping separator.

Its actual representation in the formatted String is given by the locale of the formatter instance.

For example

final DecimalFormat decimalFormat = new DecimalFormat("###,###,##0.00", DecimalFormatSymbols.getInstance(Locale.ENGLISH));

If you look at the javadoc for FmtNumber is clearly says "using the DecimalFormat class and the default locale" - so you need to have the correct default Locale for this to work.