Decimal separator in NumberFormat Decimal separator in NumberFormat java java

Decimal separator in NumberFormat


The helper class DecimalFomatSymbols is what you are looking for:

DecimalFormat format = (DecimalFormat) DecimalFormat.getInstance();DecimalFormatSymbols symbols = format.getDecimalFormatSymbols();char sep=symbols.getDecimalSeparator();

To set you symbols as needed:

//create a new instanceDecimalFormatSymbols custom=new DecimalFormatSymbols();custom.setDecimalSeparator(',');format.setDecimalFormatSymbols(custom);

EDIT: this answer is only valid for DecimalFormat, and not for NumberFormat as required in the question. Anyway, as it may help the author, I'll let it here.


I agree with biziclop and Joachim Sauer that messing with decimal and grouping separators and doing this work manually, can cause a lot of problems. Use of the locale parameter in the NumberFormat getInstance method does all the work for you automatically. And you can easily disable the thousand grouping separator, if you wish so.

The following junit test method (which passes) shows this behavior based on English and German locale.

public void testFormatter() {    DecimalFormat formatter = (DecimalFormat) DecimalFormat.getInstance(Locale.UK);    assertEquals('.', formatter.getDecimalFormatSymbols().getDecimalSeparator()); //true    formatter = (DecimalFormat) DecimalFormat.getInstance(Locale.GERMAN);    assertEquals(',', formatter.getDecimalFormatSymbols().getDecimalSeparator()); //true    //and in case you want another decimal seperator for a specific locale    DecimalFormatSymbols decimalFormatSymbols = new DecimalFormatSymbols();    decimalFormatSymbols.setDecimalSeparator('.');    formatter.setDecimalFormatSymbols(decimalFormatSymbols);    assertEquals('.', formatter.getDecimalFormatSymbols().getDecimalSeparator()); //true}


If your NumberFormat instance is a DecimalFormat, then you can use getDecimalFormatSymbols() to get to that information.

In general, you can't get to that information.

Why do you need it?