Search code examples
javacurrencydecimalformat

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


Solution

  • Currency has its custom separator. Use setMonetaryDecimalSeparator.