栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

仅Java 8的NumberFormat舍入问题

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

仅Java 8的NumberFormat舍入问题

我可以将此问题归类到

java.text.DigitList
522行。

这种情况是,它认为十进制数字

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)    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<count; ++i) {         if (digits[i] != '0') {  return true;         }     } } break;

另一个数字没有发生这种情况的原因不是

6.2088
四舍五入的结果(再次与
BigDecimal
输出进行比较
6.208800…
)。因此,在这种情况下它将四舍五入。



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/506266.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号