java8 numberformat,仅Java 8的NumberFormat舍入问题

小编典典

我可以将此问题归类到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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值