Page is a not externally linkable
- Code, Content, and Presentation
-- PHP Server Side Scripting
---- Miscalculation by PHP

PCInk - 12:13 pm on Mar 15, 2012 (gmt 0)

To put it simply why this is happening. Computers cannot store 0.1 or 0.01 as decimal fractions accurately. Imagine you are adding three numbers together in our normal decimal system using fractions and decimals:

1) One third + One third + One third = 1
2) Replace one third with decimals, this is 0.333333 rounded to six decimal places
3) 0.333333 + 0.333333 + 0.333333 = 0.999999
4) Notice, no matter how many 3's you round to, it will never actually equal one, even those it is close!
5) The more calculations you do with these rounded numbers, the greater the error of accuracy

Your computer is doing to same thing with numbers that it cannot store such as "0.01". As suggested earlier, always store currency in pennies/cents converting to dollars/pounds/euros as you print the item to the screen.

To solve your issue here, you could try to convert the numbers to pennies before any mathematical calculation. So use int(ot.value*100) - integer to get rid rogue conversion fractions. See if this works, though the output will be in cents/pennies. Then, from the cents/pennies, you can convert to dollars/pounds after all the calculations have been done.

Thread source:: http://www.webmasterworld.com/php/4428825.htm
Brought to you by WebmasterWorld: http://www.webmasterworld.com