2012-03-19 64 views
0

最近的5/10我有两个输入舍入到在PHP

min, max 

在这种情况下其中min = 32和max = 46然后我想PHP自动的值在最近的5/10即这种情况最小= 30和最大= 50?

但当然例如,如果分钟= 35或40最大= 40或45就没有必要四舍五入。

在PHP中如何做到这一点?

Btw系统只处理整数值,上面的值只是例子。它需要适用于范围从0到无穷大的一系列数字。所以4将舍入到0,6到10 ...等等...

回答

6
function round_to_10($n) { 
    return round($n/10) * 10; 
} 


php> echo round_to_10(32) 
30 
php> echo round_to_10(46) 
50 
+2

偷偷摸摸鬼鬼祟祟...... – 2012-03-19 16:08:28

+0

@KristianAntonsen OP要46 - > 50你的回答呢46 - > 45。 – Dogbert 2012-03-19 16:23:11

1

如果将数字乘以2,则可将其舍入为10秒,然后再将其除以2。

function round_five($num) { 
    return round($num*2,-1)/2; 
} 

$nums = array(32,46,35,40); 

foreach($nums as $num) { 
    printf("%s: %s\n",$num,roundFive($num)); 
} 

以上将返回

32: 30 
46: 45 
35: 35 
40: 40