[英]BigDecimal - unexpected round behaviour
我想我发现了一个错误:
MathContext mathContext = new MathContext(5, RoundingMode.HALF_UP);
result = BigDecimal.valueOf(0.004798).round(mathContext); // fails
// result is 0.004798!!! (same value)
我不得不使用以下替代方法:
BigDecimal bigDecimal = BigDecimal.valueOf(0.004798);
BigDecimal new_divisor = BigDecimal.valueOf(1, 5);
bigDecimal_array = bigDecimal.divideAndRemainder(new_divisor);
MathContext mathContext = new MathContext(5, RoundingMode.HALF_UP);
result = bigDecimal.subtract(bigDecimal_array[1], mathContext);
result = result.stripTrailingZeros();
在我看来,这个错误(如果是这样的话)是非常危险的。
不,没有错误。 您只是误解了“精度”的含义。
返回的总位数由 MathContext 的精度设置指定; 这决定了结果的精度。 数字计数从确切结果的最左边的非零数字开始。
(强调我的)。
在这种情况下,您有 4 位数字。 因此任何大于或等于 4 的精度都不会影响舍入。
与之比较
result = BigDecimal.valueOf(0.004798).round(new MathContext(3, RoundingMode.HALF_UP));
result ==> 0.00480
或与
jshell> result = BigDecimal.valueOf(1.004798).round(new MathContext(5, RoundingMode.UP));
result ==> 1.0048
哪个行为像你期望的那样。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.