Reputation: 6637
I have written below Java code to see how locales behave with numbers. I am facing with FRENCH style.
double n = 123456789.123;
System.out.println("US "+ NumberFormat.getNumberInstance(Locale.US).format(n)); //###,###.###
System.out.println("FRENCH "+ NumberFormat.getNumberInstance(Locale.FRENCH).format(n)); // # ###,##
System.out.println("GERMAN "+ NumberFormat.getNumberInstance(Locale.GERMAN).format(n)); // ###.###,##
System.out.println(NumberFormat.getNumberInstance(Locale.US).parse("123,451.23"));
System.out.println(NumberFormat.getNumberInstance(Locale.GERMANY).parse("123.451,23"));
System.out.println(NumberFormat.getNumberInstance(Locale.FRANCE).parse("123 451,23"));
OUTPUT
US 123,456,789.123
FRENCH 123 456 789,123
GERMAN 123.456.789,123
123451.23
123451.23
123
As you can see space is used as thousands separator for FRENCH locale. But when I tried to generate number "123 451,23" it does not recognize space as thousands separator.
Is this the expected behavior ?
EDIT: As a workaround I replaced space with ".". So number becomes a GERMANY format. And then convert it using that locale.
input = input.replace(" ", ".");
// Now "123 451,23" is "123.451,23" So which is same as german
System.out.println(NumberFormat.getNumberInstance(Locale.GERMANY).parse(input));
OUTPUT
123451.23
Upvotes: 1
Views: 983