【问题标题】:com.ibm.icu.text.DecimalFormat always throws ParseExceptioncom.ibm.icu.text.DecimalFormat 总是抛出 ParseException
【发布时间】:2017-09-28 13:16:07
【问题描述】:

使用以下代码,我只想允许正数。由于某种原因,我什至无法正确解析字符串:

DecimalFormat dfNoNegative = new DecimalFormat("#,##0.00");
dfNoNegative.setNegativePrefix("");
try {
    System.out.println(dfNoNegative.parse("123.00"));
} catch (ParseException e) {
    System.out.println(e.getMessage());
    System.out.println(e.getErrorOffset());
    e.printStackTrace();
}

错误信息和ErrorOffset:

Unparseable number: "123.00"
6

谁能指导我哪里错了?一个工作字符串的例子也很好

【问题讨论】:

    标签: java decimalformat parseexception


    【解决方案1】:

    我的错误是 dfNoNegative.setNegativePrefix(""); 什么都没有 ("")。这不起作用,因为字符串直接以数字开头,而123 不是"",因此它失败了。基本上这种方法会覆盖应该用作负前缀的内容(默认为-)。例如,如果将其设置为 !System.out.println(dfNoNegative.parse("!123.00")); 将打印 -123

    【讨论】:

      猜你喜欢
      • 2020-05-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-08-23
      • 2014-01-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多