3

当我想将字符串数组中的值解析为 BigDecimal 时,出现此错误:

Exception in thread "main" java.text.ParseException: Unparseable number: "86400864008640086400222"

我正在互联网上搜索如何解决此问题的解决方案。也许你们知道吗?

使用 BigDecimal 不是我的想法,但不幸的是我必须这样做。我创建了一些代码,应该将值从 String 更改为 BigDecimal 并返回它:

public static BigDecimal parseCurrencyPrecise (String value) throws ParseException
{
    NumberFormat  format = NumberFormat.getCurrencyInstance ();
    if (format instanceof DecimalFormat)
        ((DecimalFormat) format).setParseBigDecimal (true);

    Number  result = format.parse (value);
    if (result instanceof BigDecimal)
        return (BigDecimal) result;
    else {
        // Oh well...
        return new BigDecimal (result.doubleValue ());
    }
}

这是我尝试解析时的代码:

public void Function() throws ParseException {
    String [] array;
    array=OpenFile().split("\\s");
    for(int i = 10 ;i < array.length; i+= 11) {
        BigDecimal EAE = parseCurrencyPrecise(array[i]);
        System.out.println(EAE);
    }
}

OpenFile函数用数据打开文件,这样读取这个L temp+=line+" "; 这就是我用\s分割的原因。这对我来说适用于字符串和整数,但我遇到了 BigDecimal 的问题。

问候,

4

2 回答 2

3

BigDecimal(String val)您可以使用构造函数,而不是自己处理解析。来自Javadoc

BigDecimal(String val)
Translates the string representation of a BigDecimal into a BigDecimal

例如:

BigDecimal bigDecimal = new BigDecimal("86400864008640086400222");

有关构造函数采用的格式,请参见 Javadoc。

于 2015-01-05T21:17:38.660 回答
2

有什么理由不能使用构造函数吗?看起来你让它变得比它必须的更复杂。这对我有用:

System.out.println(new BigDecimal("86400864008640086400222"));
于 2015-01-05T21:23:17.447 回答