Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Decimal separator in NumberFormat

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

like image 611
Javi Avatar asked Jan 17 '11 12:01

Javi


People also ask

What is the correct decimal separator?

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.

How do you change 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.

How do you change the decimal separator in Java?

With the DecimalFormatSymbols#setDecimalSeparator() method we can change the decimal separator and with the DecimalFormatSymbols#setGroupingSeparator() method we can change the grouping separator.

What is the thousand separator in France?

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).


1 Answers

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.

like image 76
Tomas Narros Avatar answered Sep 19 '22 21:09

Tomas Narros