How to round a decimal number to nearest 5 or nearest 10 in php

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-11 00:34:54

问题


How to round a decimal number to nearest 5 or nearest 10 in php


回答1:


$new = round($num / 10, 0) * 10 rounds to nearest 10




回答2:


For the special case of nearest 10, you can use a negative precision:

$new = round($num, -1)



回答3:


Multiply by 2, round to the nearest 10 (see pascal's answer), then divide by 2. Avoid dividing/multiplying by 5 to do this since float representation will interfere with the accuracy of your results.



来源:https://stackoverflow.com/questions/3308247/how-to-round-a-decimal-number-to-nearest-5-or-nearest-10-in-php

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!