Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

round BigDecimal to nearest 5 cents

I'm trying to figure out how to round a monetary amount upwards to the nearest 5 cents. The following shows my expected results

1.03     => 1.05
1.051    => 1.10
1.05     => 1.05
1.900001 => 1.10

I need the result to be have a precision of 2 (as shown above).

Update

Following the advice below, the best I could do is this

    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)

I'm not convinced this is 100% kosher - I'm concerned precision could be lost when converting to and from doubles. However, it's the best I've come up with so far and seems to work.

like image 513
Dónal Avatar asked Jan 21 '10 02:01

Dónal


2 Answers

Using BigDecimal without any doubles (improved on the answer from marcolopes):

public static BigDecimal round(BigDecimal value, BigDecimal increment,
                               RoundingMode roundingMode) {
    if (increment.signum() == 0) {
        // 0 increment does not make much sense, but prevent division by 0
        return value;
    } else {
        BigDecimal divided = value.divide(increment, 0, roundingMode);
        BigDecimal result = divided.multiply(increment);
        return result;
    }
}

The rounding mode is e.g. RoundingMode.HALF_UP. For your examples, you actually want RoundingMode.UP (bd is a helper which just returns new BigDecimal(input)):

assertEquals(bd("1.05"), round(bd("1.03"), bd("0.05"), RoundingMode.UP));
assertEquals(bd("1.10"), round(bd("1.051"), bd("0.05"), RoundingMode.UP));
assertEquals(bd("1.05"), round(bd("1.05"), bd("0.05"), RoundingMode.UP));
assertEquals(bd("1.95"), round(bd("1.900001"), bd("0.05"), RoundingMode.UP));

Also note that there is a mistake in your last example (rounding 1.900001 to 1.10).

like image 59
robinst Avatar answered Sep 29 '22 07:09

robinst


I'd try multiplying by 20, rounding to the nearest integer, then dividing by 20. It's a hack, but should get you the right answer.

like image 40
James Cronen Avatar answered Sep 29 '22 05:09

James Cronen