【问题标题】:NumberFormat.parse() does not work for FRANCE Locale space as thousand seperatorNumberFormat.parse() 不适用于法国语言环境空间作为千位分隔符
【发布时间】:2016-02-02 09:23:12
【问题描述】:

我在下面编写了 Java 代码来查看语言环境如何处理数字。我面对的是法国风格。

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

如您所见,空格用作法语语言环境的千位分隔符。但是当我尝试生成数字“123 451,23”时,它不会将空格识别为千位分隔符。

这是预期的行为吗?

编辑: 作为一种解决方法,我用“。”替换了空格。所以数字变成了德国格式。然后使用该语言环境对其进行转换。

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

【问题讨论】:

    标签: locale number-formatting


    【解决方案1】:

    这是旧 JDK 中的一个已知问题。升级它,否则你会这个issue

    【讨论】:

      猜你喜欢
      • 2023-04-09
      • 2011-06-11
      • 2020-08-25
      • 2016-02-03
      • 1970-01-01
      • 1970-01-01
      • 2017-11-24
      • 1970-01-01
      相关资源
      最近更新 更多