我在这里面临一个难题,我不知道如何解决。我有两张不同的桌子。任务表和项目表。tasks表包含所花费的时间(以秒为单位),特定任务的每小时速率,以及该条目属于哪个项目
{id} - {seconds} - {rate} - {projectId}
现在,我正在计算像这样的特定任务的每小时计费率:
function calc ($Totalseconds, $rate){
$minutes = $Totalseconds/60; //minutes
$money = $rate * ($minutes / 60); //hours
return $money;
}
...
$money = calc($task['seconds'], $task['rate']);
$value = number_format($money,2); //ex. 1,824.69
这当然很有效。。。。例如:
task1 is 1,824.69
task2 is 24.33
etc....
只要一个项目只有几个任务,那么该项目的总计费率就是正确的。但是,当一个项目下有多个任务时,总价值与我用计算器手动添加它们时有点不同。。。因此,在另一个页面中,我通过计算特定项目的所有任务和费率来显示特定项目的总计费费率。
我从数据库中为数组中的特定项目ID"选择"所有任务,然后执行:
//array projArray has the sum of all seconds and rate for every task under a specific project
//for example a project has 10 tasks with rate $12 p/h and 2 task with $10 p/h
//so projArray has 2 arrays. One for the total time in seconds for the 10 tasks + their rate and one for the 2 tasks + their rate.
$total = 0;
foreach($projArray as $value) {
$total += calc($value['TotalTime'], $value['rate']);
}
$Totalvalue = number_format($total,2);
问题是。。。当我使用计算器手动添加特定项目的所有任务计费率时,我会得到2426.91,但最后一个函数会返回22426.92。
在这两种情况下,秒数和速率都是正确的。我不明白为什么和最终值有区别?我想我在一个项目下的任务越多,差异就越大?现在我正在测试1-50个任务。。。对于每个项目
示例数据:
a: 1,233
b: 1,233
c: 1,233
number_format((a + b + c), 2) = 3,70
number_format(a, 2) + number_format(b, 2) + number_format(c, 2) = 3,69
当您对每个任务单独调用number_format时,0003将被四舍五入。
在你的计算中可能也发生了类似的事情。