小编典典
我可以将此问题归类到java.text.DigitList522行。
这种情况是,它认为十进制数字6.0289已经舍入(与等效BigDecimal表示形式相比是正确的6.208899…),并决定不再舍入。问题在于,只有在舍入后的数字为的情况下才可以做出此决定5,而不是大于的情况下才有意义5。注意代码是如何HALF_DOWN正确区分digit=='5'和digit>'5'情况。
显然,这是一个错误,并且由于一个事实,即执行类似正确操作(仅针对另一个方向)的代码恰好位于损坏的错误代码的下面,因此这是一个奇怪的错误。
case HALF_UP:
if (digits[maximumDigits] >= '5') {
// We should not round up if the rounding digits position is
// exactly the last index and if digits were already rounded.
if ((maximumDigits == (count - 1)) &&
(alreadyRounded))
return false;
// Value was exactly at or was above tie. We must round up.
return true;
}
break;
case HALF_DOWN:
if (digits[maximumDigits] > '5') {
return true;
} else if (digits[maximumDigits] == '5' ) {
if (maximumDigits == (count - 1)) {
// The rounding position is exactly the last index.
if (allDecimalDigits || alreadyRounded)
/* FloatingDecimal rounded up (value was below tie),
* or provided the exact list of digits (value was
* an exact tie). We should not round up, following
* the HALF_DOWN rounding rule.
*/
return false;
else
// Value was above the tie, we must round up.
return true;
}
// We must round up if it gives a non null digit after '5'.
for (int i=maximumDigits+1; i
if (digits[i] != '0') {
return true;
}
}
}
break;
另一个数字没有发生这种情况的原因不是6.2088四舍五入的结果(再次与BigDecimal输出进行比较6.208800…)。因此,在这种情况下它将四舍五入。
2020-11-13