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
This symbol can be a period ("."), as is common in United States and other English-speaking countries, or a comma (","), as in continental Europe. Decimal point and decimal comma are also common names for the decimal separator.
Click File > Options. On the Advanced tab, under Editing options, clear the Use system separators check box. Type new separators in the Decimal separator and Thousands separator boxes.
With the DecimalFormatSymbols#setDecimalSeparator() method we can change the decimal separator and with the DecimalFormatSymbols#setGroupingSeparator() method we can change the grouping separator.
In a locale such as French, the comma is the decimal separator (3,01). The thousands-separator symbol can show between groups of digits in the part of the numeric value. In the default locale, the comma is the thousands separator (3,255). In a French locale, the space is the thousands separator (3 255).
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With