I have a General Ledger program that goes through and lists transaction for our customers. Basically, it get transactions from the database (data stored in MySql as float(8,2)) lists it and logs the value. While it's going through and listing the transactions, it is also adding the totals together as a balance that it also displays for each listing.
Example:
Id amount balance
101 10 10
102 15 25
103 -20.05 4.95
104 -4.95 0.00
Somtimes, when you do something like subtract 4.95 from 4.95 you will get a very small number like 1.1546319456102E-14. I've tried all types of things to fix this including type casting the variables involved as doubles and formating with sprintf. All that's left is test for this small value and if present, set the 'balance' to zero.
#here is my cluge fix
if ($ledger_balance < .01 && $ledger_balance > -.01) {
$ledger_balance = 0;
}
Anyone got any better ideas how I could deal with this situation?
Thanks,
Chuckc