Decimal separator in NumberFormat

Solution 1:

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 instance
DecimalFormatSymbols 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.

Solution 2:

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
}