当我想将String数组中的值解析为BigDecimal时,出现了这个错误:
Exception in thread "main" java.text.ParseException: Unparseable number: "86400864008640086400222"
我正在网上搜索解决这个问题的方法。也许你们知道吗?
使用BigDecimal不是我的想法,但不幸的是我必须这么做。我找到了一些代码,它应该将值从字符串更改为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函数打开数据文件并以此方式读取:temp += line + " "。这就是为令我使用\s进行分割的原因。这适用于字符串和整数,但BigDecimal会出现问题。
祝好,
BigDecimal.valueOf(result.doubleValue())
代替new BigDecimal(result.doubleValue())
。 - TomBigDecimal.valueOf(result.doubleValue())
和new BigDecimal(result.doubleValue())
,因为它们都会导致在处理一些非常大的数字时丢失精度。最好忘记NumberFormat
,直接使用以String
作为参数的BigDecimal
构造函数。 - Dawood ibn Kareem