I am really stumped on this one and it could be because I have looked at it way to long. I have 4 tables ( invoice_job , invoice_payments , invoice_credits and invoice_fc ) that I am pulling information from to return a value. My code looks like this:

<?php

//DATABASE CONNECTION
include_once("global/dbinfo.inc.php");

//GETTING BID AND ADDITIONAL AMOUNTS
$query = mysql_query("SELECT * FROM invoice_job WHERE id LIKE '194' AND jobid LIKE '53'");
while ($result = mysql_fetch_array($query)) {

$bid = $result['bidamount'];	
$additional = $result['additionalamount'];

echo "<strong>Bid Amount:</strong><br />";
echo "$";
echo number_format($bid, 2, '.', ',');

echo "<br /><br />";

echo "<strong>Additional Amount:</strong><br />";
echo "$";
echo number_format($additional, 2, '.', ',');
++$i;
}

echo "<br /><br />";

//GETTING TOTAL PAID
$query = mysql_query("SELECT SUM(amount) FROM invoice_payments WHERE jobid LIKE '53'");
while ($result = mysql_fetch_array($query)) {

$paid = $result['SUM(amount)'];

echo "<strong>Total Payments:</strong><br />";
echo "$";
echo number_format($paid, 2, '.', ',');
++$i;
}

echo "<br /><br />" ;

//GETTING TOTAL CREDITS
$query = mysql_query("SELECT SUM(amount) FROM invoice_credits WHERE jobid LIKE '53'");
while ($result = mysql_fetch_array($query)) {

$credits = $result['SUM(amount)'];	

echo "<strong>Total Credits:</strong><br />";
echo "$";
echo number_format($credits, 2, '.', ',');
++$i;
}

echo "<br /><br />" ;

//GETTING TOTAL FINANCE CHARGES
$query = mysql_query("SELECT SUM(fcamount) FROM invoice_fc WHERE jobid LIKE '53'");
while ($result = mysql_fetch_array($query)) { 

$fc = $result['SUM(fcamount)'];

echo "<strong>Total Finance Charges:</strong><br />";
echo "$";
if ($fc == "") {echo "0.00";}
else {echo number_format($fc, 2, '.', ',');}
++$i;
}

echo "<br /><br />" ;

//SHOWING TOTAL AMOUNT NOW DUE
$totaldue = (($bid+$additional+$fc)-($paid+$credits));

echo "<strong>Total Due:</strong><br />";
echo "$";
echo $totaldue;

?>

The problem is that the output looks like this:

Bid Amount:
$7,945.53

Additional Amount:
$1,100.00

Total Payments:
$8,682.83

Total Credits:
$362.70

Total Finance Charges:
$0.00

Total Due:
$-1.8189894035459E-012

But when you do the math the Total Due should come to 0.00

I am sooooooo lost here as to why this is happening, when I hard code the values rather than calling from the database everything works. I am assuming that there is a problem with the way I am formatting the data output or querying the databases. I have two questions specifically.

1: What am I doing wrong?
2: Is there an easier way to be pulling and showing this information?

Thanks in advance,
One Lost Newb...

    It could be just due to floating point inaccuracy. Just round to the required number of decimal places and it should be fine.

      laserlight wrote:

      It could be just due to floating point inaccuracy. Just round to the required number of decimal places and it should be fine.

      Yup that fixed it!

      rincewind456 wrote:

      What data type are you using to store it?

      decimal(10,2)

      Thanks guys/gals!

        Write a Reply...