php计算

时间:2012-08-06 14:18:22

标签: php

1 - 165.25
2 - 165.25
3 - 165.25
4 - 165.25
5 - 165.25
6 - 165.25
7 - 165.25
8 - 165.25
9 - 165.25
10 - 165.25

如何从所有10个金额(0.25x10)= 2.5计算.25并将该金额添加到最后一个值,如下所示:

1 - 165
2 - 165
3 - 165
4 - 165
5 - 165
6 - 165
7 - 165
8 - 165
9 - 165
10 - 167.5

$amount = 165.25; // can be any number
    $periods = 10; // can be from 1 to 100;

for($i=1;$i<=$periods;$i++){
echo $i. ' - '.$amount."<br />";
}

更新工作示例:

$amount = 165.25; // can be any number
$periods = 10; // can be from 1 to 100;

$z = 0;
for($i=1;$i<=$periods;$i++){

    $e = explode(".", $amount);

    $z += $e[1] / 100;

    if($i == $periods){
        $e[0] += $z;
    }

echo $i. ' - '.$e[0]."<br />";
}

我不喜欢爆炸任何替代品?

2 个答案:

答案 0 :(得分:2)

$amount = 165.25;
$tmp = 0;
for($i=1;$i<=9;$i++){
  $tmp = $tmp + $amount - floor($amount);
  echo $i. ' - '.floor($amount)."<br />";
}
echo $i. ' - '.$amount+$tmp."<br />";

答案 1 :(得分:0)

$total = 0;
for($i=1;$i<=10;$i++){
    $total += $amount - floor($amount);
    if($i == 10){
       echo $i . ' - ' . (floor($amount) + $total); 
    }else{
        echo $i. ' - '. floor($amount)."<br />";
    }
}

这样的事情?