51

Given a locale java.text.NumberFormat:

NumberFormat numberFormat = NumberFormat.getInstance();

How can I get the character used as Decimal separator (if it's a comma or a point) in that numberformat? How can I modify this property without having to use new DecimalFormat(format)?

Thanks

3 Answers 3

87

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.

6
  • 1
    For the record, Numberformat is an abstract class, so you'll probably actually have a DecimalFormat object anyway.
    – redcalx
    Commented Jan 17, 2011 at 12:51
  • 1
    @locster: either a DecimalFormat or a ChoiceFormat or an undocumented JDK-private NumberFormat implementation or ... Commented Jan 17, 2011 at 14:30
  • 24
    For the record, this line DecimalFormat format=DecimalFormat.getInstance(); doesn't compile as the getInstance() method returns a NumberFormat object.
    – Chexpir
    Commented Jan 16, 2013 at 11:14
  • 2
    or to be even safer; new DecimalFormat().getDecimalFormatSymbols().getDecimalSeparator()
    – pstanton
    Commented Jul 26, 2013 at 5:48
  • 4
    or take remove the dependency entirely: new DecimalFormatSymbols(Locale.getDefault(Locale.Category.FORMAT)).getDecimalSeparator()
    – pstanton
    Commented Jul 26, 2013 at 5:49
6

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
}
1

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?

9
  • 2
    I want to make possible to the user to use either comma or point as decimal separator, so I need to replace commas with points or points with commas depending on the locale used.
    – Javi
    Commented Jan 17, 2011 at 12:40
  • Good point. I overlooked that he was using NumberFormat and not DecimalFormat. Commented Jan 17, 2011 at 12:41
  • You can set the default DecimalFormatSymbols for the Locale. Commented Jan 17, 2011 at 12:43
  • 2
    This is a potentially dangerous idea, as some locales use dots for group separators and commas for decimal separator (e.g. "123.456,78"), switching them around might cause considerable confusion.
    – biziclop
    Commented Jan 17, 2011 at 12:47
  • 1
    @Javi: if that's what you want to do, then why don't you simply request the correct NumberFormat for the users locale in the first place? Messing with the NumberFormat for one locale to tweak some of its parameters to fit another locale sounds like a formidable recipe for disaster! Commented Jan 17, 2011 at 12:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.