2016-09-11 42 views
-1

转换货币后,我得到一个浮点,看起来像2.68圆形浮动小数点到PHP中的最接近的十分位数

尝试与ceil($input/10) * 10四舍五入,但这不适用于浮动。

那么是否有更简单的方法将花车四舍五入到最近的十分之一? 我想获得2.70


为我做这将是explode浮在.然后一轮68ceil更复杂的方式。然后将它们结合起来。

但是如果数字是2.96。它将需要为3.00四舍五入,所以这使得它非常复杂。

+1

如何使用'round($ input,1)'? –

回答

3

您只需拿到了数学错了,应该multipled,然后划分,而不是周围的其他方式:

ceil($input * 10)/10 

但是,只有向上舍。在每个方向上的作品用四舍五入... round

round($input * 10)/10 

别急,round有第二PARAM,只是这个! :)

round($input, 1) // round with precision of 1 digits after the decimal point