如何使用php获取此格式的floor / ceil号码?

时间:2017-11-21 02:57:59

标签: php floor ceil

如何使用php获取此格式的楼层/ ceil号码?

我希望得到这样的结果

inpput====> output
.....
3.0 =======> 3.0
3.1 =======> 3.0
3.2 =======> 3.0
3.3 =======> 3.0
3.4 =======> 3.0
3.5 =======> 4.0
3.6 =======> 4.0
3.7 =======> 4.0
3.8 =======> 4.0
3.9 =======> 4.0

............................................... ....................

所以,我使用这段代码

$x = floor($input);

但我得到了这个结果

inpput====> output
.....
3.0 =======> 3.0
3.1 =======> 3.0
3.2 =======> 3.0
3.3 =======> 3.0
3.4 =======> 3.0
3.5 =======> 3.0
3.6 =======> 3.0
3.7 =======> 3.0
3.8 =======> 3.0
3.9 =======> 3.0

............................................... ....................

然后我使用此代码

$x = ceil($input);

但我得到了这个结果

inpput====> output
.....
3.0 =======> 3.0
3.1 =======> 4.0
3.2 =======> 4.0
3.3 =======> 4.0
3.4 =======> 4.0
3.5 =======> 4.0
3.6 =======> 4.0
3.7 =======> 4.0
3.8 =======> 4.0
3.9 =======> 4.0

我该怎样做才能得到这个结果?

inpput====> output
.....
3.0 =======> 3.0
3.1 =======> 3.0
3.2 =======> 3.0
3.3 =======> 3.0
3.4 =======> 3.0
3.5 =======> 4.0
3.6 =======> 4.0
3.7 =======> 4.0
3.8 =======> 4.0
3.9 =======> 4.0

2 个答案:

答案 0 :(得分:2)

使用圆函数。前,

$x = round ($input);

参考:http://php.net/manual/en/function.round.php

答案 1 :(得分:1)

使用round()功能,然后您可以访问舍入模式。

<?php

foreach(range(3, 4, 0.1) as $i) {
   echo $i.' =======> '.number_format(round($i, 0, PHP_ROUND_HALF_UP), 1).PHP_EOL; 
}

/*
3   =======> 3.0
3.1 =======> 3.0
3.2 =======> 3.0
3.3 =======> 3.0
3.4 =======> 3.0
3.5 =======> 4.0
3.6 =======> 4.0
3.7 =======> 4.0
3.8 =======> 4.0
3.9 =======> 4.0
4   =======> 4.0
*/