Jump to content

Weird result when subtracting


tfoster

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!
Link to comment
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.

Ken
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

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.