tfoster Posted July 3, 2006 Share Posted July 3, 2006 Just wanted to see if anyone has ever run across this before.$balance = $row[totalcost];Then I am selecting checks from another table, and while fetching them, subtracting them from the balance, so:while($rowCheck = mysql_fetch_array($resultCheck)) $balance = $balance - $rowCheck[checks_amount];I get down to the last check, everything is running smoothly. The balance is 107.56 and the last check is 107.56. When I subtract the two, I am getting 3.98457E-13. What is the deal?I have found that I can get around this by adding the total of the checks and then subtracting them from the balance. I was just curious if anyone know why this is happening. It is not as if checks are being entered as 107.5600000000000045...Thanks for any feedback! Quote Link to comment https://forums.phpfreaks.com/topic/13559-weird-result-when-subtracting/ Share on other sites More sharing options...
kenrbnsn Posted July 3, 2006 Share Posted July 3, 2006 The result that you're seeing is the result of the fact that nice "normal" floating point decimal numbers can't be stored accurately as binary. What you can do is compare the result of the subtraction to a small decimal number such as 0.001 and if it is less than that, just assume the result is zero.Ken Quote Link to comment https://forums.phpfreaks.com/topic/13559-weird-result-when-subtracting/#findComment-52533 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.