Skip to content
Advertisement

How to change the decimal separator of DecimalFormat from comma to dot/point?

I have this little crazy method that converts BigDecimal values into nice and readable Strings.

JavaScript

It however, also produces a so called grouping separator "," that makes all my values come out like this:

JavaScript

I do need the separator to be a dot or a point and not a comma. Does anybody have a clue of how to accomplish this little feat?

I have read this and in particular this to death now but I cannot find a way to get this done. Am I approaching this the wrong way? Is there a much more elegant way of doing this? Maybe even a solution that accounts for different local number representations, since the comma would be perfect by European standards.

Advertisement

Answer

You can change the separator either by setting a locale or using the DecimalFormatSymbols.

If you want the grouping separator to be a point, you can use an european locale:

JavaScript

Alternatively you can use the DecimalFormatSymbols class to change the symbols that appear in the formatted numbers produced by the format method. These symbols include the decimal separator, the grouping separator, the minus sign, and the percent sign, among others:

JavaScript

currentLocale can be obtained from Locale.getDefault() i.e.:

JavaScript
User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement