我是靠谱客的博主 独特大米,最近开发中收集的这篇文章主要介绍BigDecimal的round模式BigDecimal round_down round_half_up等,觉得挺不错的,现在分享给大家,希望可以做个参考。
概述
BigDecimal round_down round_half_up等
mark round mode:
- BigDecimal.ROUND_DOWN
截端操作,类似truncate 该模式永远不会增加被操作的数的值
- BigDecimal.ROUND_UP
在精度最后一位加一个单位 setScale(2,BigDecimal.ROUND_UP) 1.666 ->1.67
1.011->1.02 1.010->1.01 该模式永远不会减少被操作的数的值
- BigDecimal.ROUND_CEILING
朝正无穷方向round 如果为正数,行为和round_up一样,如果为负数,行为和round_down一样
- BigDecimal.ROUND_FLOOR
朝负无穷方向round 如果为正数,行为和round_down一样,如果为负数,行为和round_up一样
- BigDecimal.ROUND_HALF_UP
Rounding mode to round towards {@literal "nearest neighbor"}
* unless both neighbors are equidistant, in which case round up.
* Behaves as for {@code ROUND_UP} if the discarded fraction is
* ≥ 0.5; otherwise, behaves as for {@code ROUND_DOWN}. Note
* that this is the rounding mode that most of us were taught in
* grade school.
遇到.5的情况时往上近似,例: 1.5 ->;2
- BigDecimal.ROUND_HALF_DOWN
/**
* Rounding mode to round towards {@literal "nearest neighbor"}
* unless both neighbors are equidistant, in which case round
* down. Behaves as for {@code ROUND_UP} if the discarded
* fraction is {@literal >} 0.5; otherwise, behaves as for
* {@code ROUND_DOWN}.
*/
遇到.5的情况时往下近似,例: 1.5 ->;1 注:1.51->2
- BigDecimal.ROUND_HALF_EVEN
/**
* Rounding mode to round towards the {@literal "nearest neighbor"}
* unless both neighbors are equidistant, in which case, round
* towards the even neighbor. Behaves as for
* {@code ROUND_HALF_UP} if the digit to the left of the
* discarded fraction is odd; behaves as for
* {@code ROUND_HALF_DOWN} if it's even. Note that this is the
* rounding mode that minimizes cumulative error when applied
* repeatedly over a sequence of calculations.
*/
如果舍弃部分左边的数字为奇数,则作 ROUND_HALF_UP ;如果它为偶数,则作 ROUND_HALF_DOWN
最后
以上就是独特大米为你收集整理的BigDecimal的round模式BigDecimal round_down round_half_up等的全部内容,希望文章能够帮你解决BigDecimal的round模式BigDecimal round_down round_half_up等所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复