SPUG: Rounding error

Amit Sett amitsett at gmail.com
Wed Nov 23 09:24:53 PST 2011


Hi,

I wrote some code to calculate how much change to give to a customer. Other than simply calculating the difference between the balance and the change tendered, the program also calculates how many of each bill and coin type to give. The program works well except for some cases where it short changes a customer by 1 penny. I have attached the code and two examples below (one with and the other without the rounding issue).

Any help to find out why the rounding error happens and/or suggest a better way to write the code would be appreciated.

Regards,
Amit



-------------------------------------------
example 1 (No rounding error):
-------------------------------------------
Enter the Bill Amount: 262.57
Enter the Paid Amount: 500.00
Bill total is  : $262.57
Paid amount is : $500.00
------------------------
Change comes to: $237.43
2 hundred dollar bills, 1 twenty dollar bill, 1 ten dollar bill, 1 five dollar bill, 2 one dollar bills, 1 quarter, 1 dime, 1 nickel, and 3 pennys.

---------------------------------------
example 2 (Rounding error):
---------------------------------------
Enter the Bill Amount: 262.99
Enter the Paid Amount: 500.00
Bill total is  : $262.99
Paid amount is : $500.00
------------------------
Change comes to: $237.01
2 hundred dollar bills, 1 twenty dollar bill, 1 ten dollar bill, 1 five dollar bill, and 2 one dollar bills.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.pm.org/pipermail/spug-list/attachments/20111123/e79f7552/attachment.html>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: CalculateChange.pl
Type: text/x-perl-script
Size: 1605 bytes
Desc: not available
URL: <http://mail.pm.org/pipermail/spug-list/attachments/20111123/e79f7552/attachment.bin>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.pm.org/pipermail/spug-list/attachments/20111123/e79f7552/attachment-0001.html>


More information about the spug-list mailing list