[英]PHP: increment a number by 5 each time
我正在嘗試將以下內容重現為一種。
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等
寫這個的正確方法是什么?
將10加到您的數字上,然后將結果除以10,然后將其四舍五入為最接近的(整數)整數,然后您將得到與5乘以5的數字,這將得出結果...所以...讓我們說您的電話號碼是47。
47 + 10 = 57
57/10 = 5.7
底5.7 = 5
5x5 = 25
return floor(($i + 10)/10) * 5
(($l + 10) / 10) * 5
可以解決問題
除以十(並四舍五入為整數),然后乘以5 ... 100/10 = 10 => 10 * 5 = 50
這可能會做:
$i=10;
while($l < $i){
$next = $i / 2;
$i+=10;
}
如果我正確理解了您的問題,那並不比難。
$next = floor($l/10)*5+5;
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.