【问题标题】:Parse String to Long using a specified locale (sv) and NumberFormat使用指定的语言环境 (sv) 和 NumberFormat 将字符串解析为 Long
【发布时间】:2014-10-08 10:50:59
【问题描述】:

我尝试使用以下代码在 Java 中使用瑞典语言环境将字符串 (14 123) 解析为 long:

String longString = "14 123"
NumberFormat swedishNumberFormat = NumberFormat.getInstance(new Locale("sv"));
System.out.println(swedishNumberFormat.parse(longString).longValue());

这段代码的输出是 14(应该是 14123)。根据this question,我尝试使用 sv 和 sv_SE 语言环境,但这次两种情况下的结果是相同的。

根据http://www.localeplanet.com/java/sv/index.htmlhttp://www.localeplanet.com/java/sv-SE/index.html,这两种情况下的分组分隔符都是一个空格()那么为什么要长解析的字符串不能处理一个,对于语言环境,正确格式化的双精度值存储为字符串?

【问题讨论】:

  • Java 中瑞典语语言环境中的错误?
  • 对我来说,下面的这个问题和答案比标记为相关的要好。我认为这个问题不应该结束。

标签: java string parsing locale


【解决方案1】:

瑞典语和法语一样,都需要努力。不间断的空间。

longString = longString.replace(' ', '\u00a0');

麻烦。

【讨论】:

  • @GameDroids 是的,使用不间断空格作为分组分隔符是一个完全具有误导性的设计决定。也许他们想防止在format 生成的数字上出现断字。
【解决方案2】:

你可以试试

使用手动设置的 DecimalFormat 解析该字符串。构造它,配置为有 setGroupingUsed(true), setDecimalSymbols 和你自己的 DecimalFormatSymbols

DecimalFormat df=new DecimalFormat();
df.setGroupingSize(3);
df.setGroupingUsed(true);
DecimalFormatSymbols newSymbols=new DecimalFormatSymbols();
newSymbols.setGroupingSeparator(' ');
df.setDecimalFormatSymbols(newSymbols);
df.parse(longString)

第二个选项是调试您的代码,查看您拥有的 swedishNumberFormat 实例。检查字段 groupingSize、decimalFormatSymbols.groupingSeparators。

【讨论】:

    【解决方案3】:

    this post 中所见,您需要手动将空格设置为分隔符。

     try {
          String longString = "14 123";            
          DecimalFormat decimalFormat = new DecimalFormat();  // instead of NumberFormat, use DecimalFormat
          DecimalFormatSymbols symbols = new DecimalFormatSymbols(new Locale("sv", "SE"));
          symbols.setGroupingSeparator(' ');   // set the whitespace manually as grouping seperator
          decimalFormat.setDecimalFormatSymbols(symbols);           
          System.out.println(svSE.parse(longString)); 
      } catch (ParseException ex) {
          Logger.getLogger(Playground.class.getName()).log(Level.SEVERE, null, ex);
      }
    
        //> output is 14123
    

    说实话我有点困惑,但我认为 问题 是,您需要 格式化字符串(显然这里的每个空格都不相同)

     try {
          long testNumber = 123987l; 
          NumberFormat swedishNumberFormat = NumberFormat.getInstance(new Locale("sv")); 
    
          //here I format the number into a String
          String formatedString = swedishNumberFormat.format(testNumber);  
          System.out.println(formatedString);  // result: "123 987"
    
          // when parsing the formated String back into a number
          System.out.println(swedishNumberFormat.parse(formatedString));  // result: "123987"
    
          // but when parsing a non formated string like this one
          System.out.println(swedishNumberFormat.parse("123 987")); // result "123"
     } catch (ParseException ex) {
          Logger.getLogger(Playground.class.getName()).log(Level.SEVERE, null, ex);
     }
    

    如果我在这里错了或者我的例子不起作用,请纠正我。我不知道它为什么会这样,但是为了避免像上面这样的混乱情况,您可能需要手动设置分隔符。

    编辑

    正如 Joop Eggen 在他的 answer 中所说,字符串需要使用硬的、不间断的空格 ('\u00a0') 而不是简单的空格。这就是我的示例在最后一行返回“123”的原因(我只使用了普通空格)。

    【讨论】:

      猜你喜欢
      • 2015-01-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-09-25
      • 1970-01-01
      • 1970-01-01
      • 2014-03-15
      • 2014-02-01
      相关资源
      最近更新 更多