Mark
asked on
BigDecimal rounding problem using setScale()
Things aren't working the way I expect. I have the code:
activeAmount.setScale(2,Ro undingMode .HALF_UP);
pensionAmount.setScale(2,R oundingMod e.HALF_UP) ;
interestAmount.setScale(2, RoundingMo de.HALF_UP );
distributionAmount.setScal e(2,Roundi ngMode.HAL F_UP);
BigDecimal balance = pensionAmount.add(activeAm ount).add( interestAm ount).subt ract(distr ibutionA$
balance.setScale(2,Roundin gMode.HALF _UP);
wrk = string12 + twoPlaces.format(balance);
System.out.println("Balanc e " + 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?
activeAmount.setScale(2,Ro
pensionAmount.setScale(2,R
interestAmount.setScale(2,
distributionAmount.setScal
BigDecimal balance = pensionAmount.add(activeAm
balance.setScale(2,Roundin
wrk = string12 + twoPlaces.format(balance);
System.out.println("Balanc
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER