内容简介:翻译自:https://stackoverflow.com/questions/11612045/correct-sums-with-dividing-sums-countering-rounding-errors
编写的带有 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.34,3.33,3.33.
有些人可能认为,如果有足够的小数,这只是一个问题,当有大量的行时.但在一个经济的系统中,我认为从一开始就拥有一个故障安全系统是很重要的.它需要是可扩展的,并且不会有任何最小的错误.不公平是好的,只是没有错误.
类似的问题: sum divided values problem (dealing with rounding error)
.
以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/ .
翻译自:https://stackoverflow.com/questions/11612045/correct-sums-with-dividing-sums-countering-rounding-errors
以上所述就是小编给大家介绍的《php – 使用除以总和的正确总和,抵消舍入误差》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!
猜你喜欢:- PHP警告:除以零
- LeetCode39.组合总和 JavaScript
- N的总和列出了元素方式的python
- LeetCode - 112 - 路径总和(path-sum)
- AI只是数据科学部件的总和,我们的AI必须做什么?
- LeetCode 之 JavaScript 解答第112题 —— 路径总和(Path Sum)
本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们。
Python Data Structures and Algorithms
Benjamin Baka / Packt Publishing / 2017-5-30 / USD 44.99
Key Features A step by step guide, which will provide you with a thorough discussion on the analysis and design of fundamental Python data structures.Get a better understanding of advanced Python c......一起来看看 《Python Data Structures and Algorithms》 这本书的介绍吧!