NumberFormat.parse()不适用于法国语言环境空间,因为千位分隔符 [英] NumberFormat.parse() does not work for FRANCE Locale space as thousand seperator

查看:166
本文介绍了NumberFormat.parse()不适用于法国语言环境空间,因为千位分隔符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Java代码下面编写了代码,以查看语言环境如何与数字一起表现.我正面临着法国风格.

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"));

输出

US              123,456,789.123
FRENCH          123 456 789,123
GERMAN          123.456.789,123
123451.23
123451.23
123

如您所见,空格被用作FRENCH语言环境的千位分隔符.但是,当我尝试生成数字"123 451,23"时,它无法将空格识别为千位分隔符.

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.

这是预期的行为吗?

解决方法是将空格替换为.".这样数字就变成了德国格式.然后使用该语言环境进行转换.

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));

输出

123451.23

推荐答案

这是旧JDK中的一个已知问题.升级它,否则您将问题

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

这篇关于NumberFormat.parse()不适用于法国语言环境空间,因为千位分隔符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆