Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java i18n: Where to get the decimal grouping character for a locale

Question: Where do I have to look when I want to know which character will be used for decimal grouping when using a given locale?

I tried the following code:

Locale locale = new Locale("Finnish", "fi");
DecimalFormat format = (DecimalFormat) DecimalFormat.getInstance(locale);
System.out.println(format.format(12345678.123));

Problem here is, that the parameters for the locale don't seem to be correct but I anyway think there must be some place where this information can be looked up without writing programs for it.

It seems the stuff is not taken from the regional settings on my machine as I changed the characters for the german language in the windows control panel but there was no change within the java program.

like image 852
Mathias Weyel Avatar asked Dec 18 '22 03:12

Mathias Weyel


2 Answers

EDIT: The problem is the way you're constructing your Locale. Try just:

Locale locale = new Locale("fi");

To answer the question about how you'd get the separator programmatically, you'd use:

Locale locale = new Locale("fi");
DecimalFormat format = (DecimalFormat) NumberFormat.getInstance(locale);
DecimalFormatSymbols symbols = format.getDecimalFormatSymbols();
char groupingChar = symbols.getGroupingSeparator();
like image 189
Jon Skeet Avatar answered Dec 19 '22 16:12

Jon Skeet


The comment for looking in the source had been too challenging...

Here's where the stuff is to be found:

Within the rt.jar of the jdk, in the package sun.text.resources there are files "FormatData_[ISO-Code].class" files in which the character for decimal grouping and the other related values are contained and can be viewed by decompiling them. The Java Decompiler (http://java.decompiler.free.fr) has been very helpful on that one.

like image 40
Mathias Weyel Avatar answered Dec 19 '22 18:12

Mathias Weyel