微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

php – 使用除以总和的正确总和,抵消舍入误差

PHP编写的带有MysqL数据库的Web应用程序.

我有一个系统,可以在分摊成本时为许多人计算不同的成本.例如,人员A购买的东西为10,而人员B,C和D应该分摊成本.

因此,该系统应该记录A人10的正记录和B,C和D的10/3的负记录.

但是,当这样做时;四舍五入后B,C和D均为-3.33.当然这总计不到10个.这个问题的最佳解决方法是什么?一个最佳的解决方案是随机一个人得到的稍微大一点的成本.

一种可能的解决方案是,如果我只让最后一个人的债务为10 – (A B),但如果四个人分摊成本例如13.34则会出现问题.那么不同的部分将是3.34,3.34,3.34和3.32,而最佳分割将是3.34,3.33,3.33.

有些人可能认为,如果有足够的小数,这只是一个问题,当有大量的行时.但在一个经济的系统中,我认为从一开始就拥有一个故障安全系统是很重要的.它需要是可扩展的,并且不会有任何最小的错误.不公平是好的,只是没有错误.

类似的问题:sum divided values problem (dealing with rounding error)

这似乎工作 – http://jsfiddle.net/nQakD/.

以jQuery为例,但如果你了解PHP,你应该可以轻松地将它转换为PHP.如果你还需要PHP代码,告诉我,我会为你写的.

我也会在这里粘贴代码

$(document).ready(function() {
    var price = 17.48,people = 4,payment = (price/people).toFixed(2),count=0;
    var payments = [];
    for(i = 0; i < people; i++) {
       payments.push(payment);   
    }

    if(payment*people != price) {
        var currentPayment = payment*people;

        $(payments).each(function() {
            if(currentPayment < price) {
                currentPayment = (currentPayment-this).toFixed(2);
                var newPayment = parseFloat(this)+0.01;
                payments[count] = newPayment.toFixed(2);
                currentPayment = parseFloat(currentPayment)+parseFloat(newPayment);
            }
            else if(currentPayment > price) {
                currentPayment = (currentPayment-this).toFixed(2);
                var newPayment = parseFloat(this)-0.01;
                payments[count] = newPayment.toFixed(2);
                currentPayment = parseFloat(currentPayment)+parseFloat(newPayment);
            }
            count++;
        });   

    }  
    $(payments).each(function() {
        $("#result").append("<b>"+this+"</b><br/>");
    });       
});​

编辑:

这是工作的PHP代码

$price = 13.34;
$people = 4;
$payment = (float)$price/$people;
$payment = 0.01 * (int)($payment*100);
$count = 0;
$payments = Array();
for($i = 0; $i < $people; $i++) {
    array_push($payments,$payment);
}
if($payment*$people != $price) {
    $currentPayment = $payment*$people;
    foreach($payments as $pay) {
        if($currentPayment < $price) {
            $currentPayment = $currentPayment-$pay;
            $currentPayment = 0.01 * (int)($currentPayment*100);               
            $newPayment = (float)$pay+0.01;
            $newPayment = 0.01 * (int)($newPayment*100);
            $payments[$count] = $newPayment;
            $currentPayment = (float)$currentPayment+$newPayment;
        }
        else if($currentPayment > $price) {
            $currentPayment = $currentPayment-$pay;
            $currentPayment = 0.01 * (int)($currentPayment*100);               
            $newPayment = (float)$pay-0.01;
            $newPayment = 0.01 * (int)($newPayment*100);
            $payments[$count] = $newPayment;
            $currentPayment = (float)$currentPayment+$newPayment;
        }
        $count++;
    }
}
foreach($payments as $payed) {
    echo '<b>'.$payed.'</b><br />';
}​​​

编辑2:

这应该修复js问题 – 上面的http://jsfiddle.net/nQakD/更新代码.

编辑3:

编辑了PHP代码和JS代码,因此它适用于所有示例 – http://jsfiddle.net/nQakD/.

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐