PHP / SQL数学有疯狂的结果

时间:2014-12-19 03:26:41

标签: php mysql

这是代码;


    //calculate parts total
    $sqlparts = "SELECT jobNumber, SUM(unitPrice*quantity) AS c FROM s_partOrders WHERE jobNumber = $jobNumber";
    $rowparts = mysqli_query($con, $sqlparts) or die('Action failed: ' . mysqli_error($con));
    $rsparts = mysqli_fetch_assoc($rowparts);
    $partsTotal = $rsparts[c];
    if(empty($rsparts[c]))
    {
        $partsTotal = 0;
    }

    // calculate payments total
    $serviceTotal = $row[costLabour]+$row[costCallout]+$row[costDelivery]+$partsTotal;
    $sqlpayments = "SELECT jobNumber, amount, SUM(amount) AS sumAmount FROM s_payments WHERE jobNumber = $jobNumber";
    $rowpayments = mysqli_query($con, $sqlpayments) or die('Action failed: ' . mysqli_error($con));
    $rspayments = mysqli_fetch_assoc($rowpayments);
    $paymentsTotal = $rspayments[sumAmount];
    if(empty($rspayments[sumAmount]))
    {
        $paymentsTotal = 0;
    }
    $amountDue = $serviceTotal - $paymentsTotal;
    $gst = round($serviceTotal/11, 2);
    $exGST = $serviceTotal-$gst;

    echo "($partsTotal + $row[costLabour] + $row[costCallout] + $row[costDelivery]) - $paymentsTotal = $amountDue";

这是输出结果!

(1850 + 1321.14 + 0 + 0) - 3171.14 = 4.54747350886E-13

它只发生在一些记录上。其他人计算完美。有点奇怪! 所有值都存储在数据库中double

1 个答案:

答案 0 :(得分:1)

哈哈,对不起,不过是你,而不是结果,疯狂:)

4.54747350886E-13基本上是0(忽略精确到十二进制小数值),这就是(1850 + 1321.14 + 0 + 0) - 3171.14计算到的内容。这个结果没有错。

4.54747350886E-13 = 0.0000000000005