Java四舍五入BigDecimal值,在2个级别保留2位小数
decimalValue=10.2345
在使用下面的代码进行舍入后给出10.23
decimalValue.setScale(2, RoundingMode.HALF_UP)
我需要将值显示为10.24
。找到了一种在两个级别进行舍入的解决方案,例如先舍入到3位数,然后舍入到2位数,如下所示
decimalValue.setScale(3, RoundingMode.HALF_UP).setScale(2, RoundingMode.HALF_UP)
请建议是否有更好的解决方案
# 1 楼答案
RoundingMode.HALF_UP
将对.5
及以上的数据进行汇总HALF_UP
改用
RoundingMode.UP