在PHP

时间:2016-09-11 11:36:34

标签: php

转换货币后,我得到的浮动看起来像2.68

尝试用ceil($input / 10) * 10进行四舍五入,但这对浮点数不起作用。

那么有一种更简单的方法可以将浮点数舍入到最接近的第十位吗? 我想获得2.70

对我来说,更复杂的方法是explode .上的浮动,然后是68 ceil。然后将它们结合起来。

但是如果数字是2.96则是什么。它需要被3.00四舍五入,所以这使它变得非常复杂。

1 个答案:

答案 0 :(得分:4)

你只是弄错了数学,它应该是多重的,然后是分数,而不是相反:

ceil($input * 10) / 10

但这只是四舍五入。每个方向的舍入与round ...

一起使用
round($input * 10) / 10

但等等,round有第二个参数,就这个! :)

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