Link to home
Start Free TrialLog in
Avatar of Mark
Mark

asked on

BigDecimal rounding problem using setScale()

Things aren't working the way I expect. I have the code:

    activeAmount.setScale(2,RoundingMode.HALF_UP);
    pensionAmount.setScale(2,RoundingMode.HALF_UP);
    interestAmount.setScale(2,RoundingMode.HALF_UP);
    distributionAmount.setScale(2,RoundingMode.HALF_UP);
    BigDecimal balance = pensionAmount.add(activeAmount).add(interestAmount).subtract(distributionA$
    balance.setScale(2,RoundingMode.HALF_UP);
    wrk = string12 + twoPlaces.format(balance);
    System.out.println("Balance             " + wrk.substring(wrk.length() - 12));
System.out.println("\n" + fourPlaces.format(balance));

And I get:

DROP TOTALS:
  Active               21,443.25  Last Active Deposit: 04/23/2011
  Pension             125,534.07
Total Contributions   146,977.32
  Interest              2,512.78  Current Interest Rate: 1.7003%
  Distribution        149,490.10
Balance                    -0.00

-0.0026

Why is the 0.0026 not rounded off to 2 decimal positions?
ASKER CERTIFIED SOLUTION
Avatar of mccarl
mccarl
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Mark
Mark

ASKER

That did the trick! Thanks. I think I knew that once, but forgot.