Hey, I've got some values that don't seem to be calculating correctly. I've got two numbers, subtracting one from another, and instead of the answer being an exact number, like .06, it's coming out as like .0599999999.
For some background, I'm working with monetary values, and since I'm so new to php and don't know of a better way, I'm simply converting all numbers to cents in the database by multiplying everything by 100, and if I'm spitting it out to the screen for the user, I'm multiplying it by 100. So anyway, I'm taking one number, say $2000 (which is stored in the database as 200000), and assigning it to a variable say called $net.
The other number is being obtained from the sum of numbers in a table, and I'm calling it $sum.
So here's how I'm getting the $net variable. The value in the table that it is pulling is 200000:
$query = mysql_query("SELECT * FROM mytable WHERE id=1") or die(mysql_error());
while($row = mysql_fetch_array($query))
{
$net=$row['net']/100;
}
So at this point, $net=2000. Here's how I get the $sum variable:
$query = "SELECT SUM(allotment) FROM anothertable";
$result = mysql_query($query) or die(mysql_error());
while($row = mysql_fetch_array($result))
{
$sum=$row['SUM(allotment)']/100;
}
In this case, $sum=1999.94
So now I want to create a value called $leftover that is equal to $net minus $sum.
So I do this:
$leftover=$net-$sum;
It should end up being .06, but it's not, it's ending up as .059999999. Any clues as to why?
Now I noticed that if in my while statement I set the variables like this:
$sum=$row['SUM(allotment)'];
And
$net=$row['net'];
Then calculate the leftover amount from those like this:
$leftover=($net-sum)/100;
Then it works! Why?
FYI - I even printed out my variables as I went along to check them. Sure enough, it prints out:
$net=2000
$sum=1999.94
So 2000-1999.94 should equal 0.06 right? I just don't understand!