Daniel Schmidt
Daniel Schmidt

Reputation: 139

Currency Decimal Separator not working

I have a strange behaviour, I use a DecimalFormat to convert a double to a currency value and will change the decimal separator, but it seems that it will ignore the decimal separator in any case.

class Currency {
  public static void main (String... args) {
     DecimalFormat nf =(DecimalFormat) DecimalFormat.getCurrencyInstance(Locale.getDefault());
     nf.setGroupingUsed(true);

     DecimalFormatSymbols symbols = nf.getDecimalFormatSymbols();
     symbols.setGroupingSeparator(' ');
     symbols.setCurrencySymbol("EUR");
     symbols.setDecimalSeparator('-');
     nf.setDecimalFormatSymbols(symbols);

     System.out.println(nf.format(13000.31));
  }
}

so i would expect that the result should be: 13 000-31 EUR if i am in a german locale, if i am in a US locale it should be EUR 13 000-31. But what i see is 13 000,31 EUR or EUR 13 000.31.

It works well with the currency symbol and with the grouping separator but the decimal Separator will ignore in any case.

Has anybody an idea why?! Thanks

Upvotes: 3

Views: 877

Answers (2)

Dariusz
Dariusz

Reputation: 22241

Currency has its custom separator. Use setMonetaryDecimalSeparator.

Upvotes: 4

Abacus
Abacus

Reputation: 19421

For currencies, there is an extra field to set the decimal separator:

symbols.setMonetaryDecimalSeparator('-');

Upvotes: 4

Related Questions