Possible Duplicate:
Practices to limit floating point accuracy problems
When I am adding a list of new totalSubs to a running total, PHP returns a weird value.
I have generated the below output (see line 40):
1 The total currently '0' (integer) + new TotalSub '-26969.55' type(double) = total '-26969.55'
2 The total currently '-26969.55' (double) + new TotalSub '249.6' type(double) = total '-26730.05'
...
39 The total currently '-164.89' (double) + new TotalSub '61.95' type(double) = total '-112.94'
40 The total currently '-102.94' (double) + new TotalSub '98.71' type(double) = total '-5.3300000000009'
41 The total currently '-5.3300000000009' (double) + new TotalSub '50' type(double) = total '45.769999999999'
The PHP generating is:
echo ($count++) . " The total currently '$totalTrans' (".gettype($totalTrans).") + new TotalSub '$totalVar' type(".gettype($totalVar).") = total '" . ($totalTrans + $totalVar) ."'<br />";
How can I fix the 00000000009?
From what I can see.
Try multiplying the input data by 100 and then dividing the totals by 100 at the end.
Multiplting by 1 or 10 will most likely not correct it which might be due to the 2 decimals and by using 100 the values are now whole numbers (ints)
Remember floating-point data types (such as DOUBLE) do not represent exact numbers and are approximate.
You could use
bcmath()for maths calculations in the future as usingnumber_format() is no different than usinground() without returning it as a string.http://php.net/manual/en/book.bc.php