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

本秂侑毒 提交于 2019-12-11 13:42:56

问题


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

回答1:


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



来源:https://stackoverflow.com/questions/33478546/numberformat-parse-does-not-work-for-france-locale-space-as-thousand-seperator

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!