简体   繁体   English

Numberformat.parse对于Java中的法语语言环境无法正常工作

[英]Numberformat.parse not working correctly for french locale in java

In my code, I am getting the locale value and using it in the below snippet of code : 在我的代码中,我正在获取语言环境值,并在下面的代码片段中使用它:

    NumberFormat nf = NumberFormat.getInstance(locale);
    double value = nf.parse(input).doubleValue();

For en_gb locale and fr_fr locale the input provided is the same ie, 1,000.00. 对于en_gb语言环境和fr_fr语言环境,提供的输入是相同的,即1,000.00。 But the "value" is 1000.0 for en_gb locale and 1.0 for fr_fr locale. 但是对于en_gb语言环境,“值”是1000.0,对于fr_fr语言环境,“值”是1.0。

Did try to search for solutions in the below posts : 1] NumberFormat.parse() does not work for FRANCE Locale space as thousand seperator 2] http://bugs.java.com/view_bug.do?bug_id=6318800 是否尝试在以下帖子中搜索解决方案:1] NumberFormat.parse()不适用于法国语言环境空间,因为千位分隔符 2] http://bugs.java.com/view_bug.do?bug_id=6318800

but no luck. 但没有运气。 Any thoughts/pointers on this will be really helpful. 任何对此的想法/指针都将非常有帮助。

You're parsing a string, which is '1,000.00'. 您正在解析一个字符串,即“ 1,000.00”。 The problem is, in France locale, the correct format of number 1000 is '1 000'. 问题是,在法国语言环境中,数字1000的正确格式为'1 000'。 That's why you ended up getting an output of '1'. 这就是为什么您最终得到输出“ 1”的原因。

An simple example is: 一个简单的例子是:

NumberFormat nf = NumberFormat.getInstance(Locale.FRANCE);

double d = 1000.00;
System.out.println(nf.format(d));

NumberFormat nf1 = NumberFormat.getInstance(Locale.UK);

System.out.println(nf1.format(d));

The output for France is '1 000' whereas for UK it's '1,000', which means in France, they use space(note its not the same space we can specify by ' ') instead of comma to separate thousand. 法国的输出为'1 000',而英国的输出为'1,000',这意味着在法国,他们使用空格(请注意,我们可以通过''指定的空格)而不是逗号来分隔千位。

In your case, since the input is a string representation of a number, which is '1,000.00', this means it's locale specific and you need to use a right locale format to parse it. 在您的情况下,由于输入是数字的字符串表示形式,即“ 1,000.00”,这意味着它是特定于语言环境的,并且您需要使用正确的语言环境格式来解析它。 This implies that your application should be aware of it's locale from the context. 这意味着您的应用程序应该从上下文中知道它的语言环境。 Usually the NumberFormat class will just use the default locale, depending on where you run this application and for most cases, that's sufficient. 通常, NumberFormat类将仅使用默认语言环境,具体取决于您在哪里运行此应用程序,并且在大多数情况下,这已经足够。

You do need to provide more detail on what you trying to do and why you're receiving a string as an input. 您确实需要提供有关您要执行的操作以及为什么要接收字符串作为输入的更多详细信息。

UPDATE 更新

try this: 尝试这个:

DecimalFormatSymbols dfs = DecimalFormatSymbols.getInstance(Locale.UK);
DecimalFormat decimalFormat = new DecimalFormat();
decimalFormat.setDecimalFormatSymbols(dfs);

System.out.println(decimalFormat.parse("1,000"));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM