You have run into a bad case of rounding error. doubles are not exactly
the number you wish to represent, and thus, amountPaid - totalCost may
be a tiny bit less then 9, which gets rounded down to 8 by the int cast.
> double totalCost = 3.91, amountPaid = 4.00;
> int totalChange;
[quoted text clipped - 3 lines]
>
> it keeps saying the answer is 8... can any one help?
Try (int) (Math.round(100 * (amountPaid - totalCost)))
Or better use, store the amount as an int/long in pennies
or use BigDecimal to avoid floating point precision issues.
Roedy Green - 30 Jan 2006 14:40 GMT
>Try (int) (Math.round(100 * (amountPaid - totalCost)))
>
>Or better use, store the amount as an int/long in pennies
>or use BigDecimal to avoid floating point precision issues.
for more discussion of the problem of currency calculation see
http://mindprod.com/jgloss/currency.html

Signature
Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.