我试图弄清楚如何将货币金额向上舍入到最接近的 5 美分。以下显示了我的预期结果
1.03 => 1.05
1.051 => 1.10
1.05 => 1.05
1.900001 => 1.10
我需要结果的精度为 2(如上所示)。
更新
按照下面的建议,我能做的最好的就是这个
BigDecimal amount = new BigDecimal(990.49)
// To round to the nearest .05, multiply by 20, round to the nearest integer, then divide by 20
def result = new BigDecimal(Math.ceil(amount.doubleValue() * 20) / 20)
result.setScale(2, RoundingMode.HALF_UP)
我不相信这是 100% 洁净的 - 我担心在转换为双打时可能会丢失精度。但是,这是迄今为止我想出的最好的,并且似乎有效。