如何使用 php floor/ceil 数字来获取此格式?

How to floor/ceil number to get this format using php?

如何使用 php floor/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

使用圆形函数。例如,

$x = round ($input);

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

请改用 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
*/