反向税收计算器-缺少一分钱

时间:2018-07-23 11:04:59

标签: precision reverse-engineering floating-accuracy calculation tax

我在调换商品的价格/税金时遇到问题,我们缺了几分钱。

示例:

enter image description here

enter image description here

如您所见,一分钱不见了(73比72.99)

我有2种不同的功能:

 function revertTaxes($amount, $vtps = NULL, $vtvq = null) {

        $ratio = 1 + ((1 * $vtps) + (1 * $vtvq));
        $amount_without_taxes = round(($amount / $ratio), 2);
        $tps = round($amount_without_taxes * $vtps, 2);
        $tvq = round($amount_without_taxes * $vtvq, 2);
        $amount_without_taxes = $amount - $tps - $tvq;

        $data = array('amount_without_taxes' => round(ceil($amount_without_taxes*1000)/1000,2),
        'tps' => round($tps, 2),
        'tvq' => round($tvq, 2));
    return $data;
}

还有税收计算器:

 function calculate_price_with_taxes($amount, $tps, $tvq, $return_array = FALSE) {

    $tps_amount = 0;
    $tvq_amount = 0;
    $total_amount = $amount;

        $price_with_tps = $amount;
        if ($tps) {
            $tps_amount = round($amount * $tps,2);
            $price_with_tps = $amount + ($amount * $tps);
        }

        $price_with_tvq = $price_with_tps;
        if ($tvq) {
            $tvq_amount = round($amount * $tvq,2);
            $price_with_tvq = $price_with_tps + ($amount * $tvq);
        }
        $total_amount = $price_with_tvq;

    if($return_array===TRUE){
        $total_amount -  round(ceil($total_amount*1000)/1000,2);  
        return array("total_avec_taxes"=>$total_amount,"tps"=>$tps_amount,"tvq"=>$tvq_amount,"total_sans_taxe"=>$amount);
    }
    return $price_with_tvq;
}

我知道这是一个精度问题,但无法弄清楚如何解决它,尤其是当我得到(0,333333333333)

0 个答案:

没有答案