当我想将字符串数组中的值解析为 BigDecimal 时出现此错误:
Exception in thread "main" java.text.ParseException: Unparseable number: "86400864008640086400222"
我正在 Internet 上搜索如何解决此问题的解决方案。也许你们知道?
使用 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 时遇到了问题。
问候,
最佳答案
您可以使用 BigDecimal(String val)
构造函数,而不是自己处理解析。来自Javadoc
BigDecimal(String val)
Translates the string representation of a BigDecimal into a BigDecimal
例如:
BigDecimal bigDecimal = new BigDecimal("86400864008640086400222");
有关构造函数采用的格式,请参阅 Javadoc。
关于java - 如何在不出错的情况下将 String 解析为 BigDecimal,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27787905/