我想写一个解析器,将字符串转换为大十进制。要求它 100% 准确。(嗯,我目前正在编程是为了好玩。所以我宁愿要求它... ;-P(
所以我想出了这个程序:
public static BigDecimal parse(String term) {
char[] termArray = term.toCharArray();
BigDecimal val = new BigDecimal(0D);
int decimal = 0;
for(char c:termArray) {
if(Character.isDigit(c)) {
if(decimal == 0) {
val = val.multiply(new BigDecimal(10D));
val = val.add(new BigDecimal(Character.getNumericValue(c)));
} else {
val = val.add(new BigDecimal(Character.getNumericValue(c) * Math.pow(10, -1D * decimal)));
decimal++;
}
}
if(c == '.') {
if(decimal != 0) {
throw new IllegalArgumentException("There mustn't be multiple points in this number: " + term);
} else {
decimal++;
}
}
}
return val;
}
所以我尝试了:
parse("12.45").toString();
我以为它会12.45
.相反,它12.45000000000000002498001805406602215953171253204345703125
.我知道这可能是由于二进制表示的限制。但是我该如何解决这个问题呢?
注意:我知道你可以只使用new BigDecimal("12.45");
。但这不是我的意思——我想自己写,不管这有多愚蠢。
是的,这是由于二进制表示的局限性。 任何 10 的负幂都不能完全表示为double
。
要解决此问题,请将所有double
算术替换为所有BigDecimal
算术。
val = val.add(
new BigDecimal(Character.getNumericValue(c)).divide(BigDecimal.TEN.pow(decimal)));
有了这个,我得到了12.45
.
这可以稍微改进一下。只除一次。只需忽略循环中的小数点即可。只需计算小数。所以"12.45"
变得1245
decimal == 2
.现在在最后,您只需将其除以,在这种情况下,BigDecimal.TEN.pow(2)
(或 100(即可获得12.45
.
public static BigDecimal parse(String term)
{
char[] termArray = term.toCharArray();
// numDecimals: -1: no decimal point at all, so no need to divide
// 0: decimal point found, but no digits counted yet
// > 0: count of digits after decimal point
int numDecimals = -1;
BigDecimal val = new BigDecimal.ZERO;
for(char c: termArray)
{
if (Character.isDigit(c))
{
val = val.multiply(BigDecimal.TEN).add(BigDecimal.valueOf(Character.getNumericValue(c)));
if (numDecimals != -1)
numDecimals++;
}
else if (c == '.')
{
if (numDecimals != -1)
throw new IllegalArgumentException("There mustn't be multiple points in this number: " + term);
else
numDecimals = 0;
}
}
if (numDecimals > 0)
return val.divide(BigDecimal.TEN.pow(numDecimals));
else
return val;
}
请注意,此函数不适用于负值,也无法识别科学记数法。为此,使用原始字符串,索引和charAt(index)
可能比当前循环更理想。但这不是问题所在。