PHP:每次增加一个数字5

时间:2012-01-28 08:33:02

标签: php math

我正在尝试将以下内容重现为单行。

if($l < 10) $next = 5; return;
if($l < 20) $next = 10; return;
if($l < 30) $next = 15; return;
if($l < 40) $next = 20; return;
if($l < 50) $next = 25; return;
if($l < 60) $next = 30; return;
if($l < 70) $next = 35; return;
if($l < 80) $next = 40; return;
if($l < 90) $next = 45; return;
if($l < 100) $next = 50; return;

(在语法上不正确,但你明白了)

因此,如果数字小于10,则$ next为5,如果数字小于20则为10。

$next = ((round($l, -1)-5));尽可能接近它,但这会给出

5
15
25
35
45
55
65
75
85

不是理想的5,10,15,20等等。

写这个的正确方法是什么?

5 个答案:

答案 0 :(得分:4)

将10添加到您的数字,然后将结果除以10并将其向下舍入到最接近的(floor)整数,然后您将得到乘以5的数字,这将产生您的结果...所以.. 。 比方说你的号码是47。

47 + 10 = 57

57/10 = 5.7

楼层5.7 = 5

5x5 = 25

return floor(($i + 10)/10) * 5

答案 1 :(得分:1)

(($l + 10) / 10) * 5会做到这一点

答案 2 :(得分:0)

DIVDIDE到10(并将其舍入为int),然后乘以5 ... 100/10 = 10 =&gt; 10 * 5 = 50

答案 3 :(得分:0)

这可能会:

$i=10;
while($l < $i){
    $next = $i / 2;
    $i+=10;
}

答案 4 :(得分:0)

如果我正确理解你的问题,那就不比;

$next = floor($l/10)*5+5;