Kaushik Lele
Kaushik Lele

Reputation: 6637

NumberFormat.parse() does not work for FRANCE Locale space as thousand seperator

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

Answers (1)

ergonaut
ergonaut

Reputation: 7057

This is a known issue in old JDKs. Upgrade it or you will this issue

Upvotes: 1

Related Questions