Jump to content


This topic is now archived and is closed to further replies.


Weird result when subtracting

Recommended Posts

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!

Share this post

Link to post
Share on other sites
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.


Share this post

Link to post
Share on other sites


Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.