如何在PHP中舍入数字?

时间:2012-07-25 15:51:12

标签: php numbers rounding

  

可能重复:
  Round up to nearest multiple of five in PHP

我必须完成数字,但不是传统方式。我想要圆到5,10,15,20,25,30,35,40,45,50等等。舍入后,舍入的数字应该落在上面列出的数字之一。

有可能吗?

2 个答案:

答案 0 :(得分:3)

试试这个:

$rounded_value = round($original_value/5) * 5;

或者如果总是四舍五入:

$rounded_value = floor($original_value/5) * 5;

或者如果总是四舍五入:

$rounded_value = ceil($original_value/5) * 5;

答案 1 :(得分:2)

查看http://www.php.net/manual/en/function.round.php#32008

<?php 
// Rounding to the nearest fifth 
// or any other increment you wish... 

$percent = "48"; 
  $num = round($percent/5)*5; 
    echo $num; 
    // returns 50 

$percentt = "47"; 
  $numm = round($percentt/5)*5; 
    echo $numm; 
    // returns 45 
?>
相关问题