How to round a decimal number to nearest 5 or nearest 10 in php
Asked
Active
Viewed 1,210 times
3 Answers
4
$new = round($num / 10, 0) * 10
rounds to nearest 10

Dennis Haarbrink
- 3,738
- 1
- 27
- 54
-
If this is an optimal solution for your problem please flag the answer. – Dennis Haarbrink Jul 22 '10 at 11:50
1
For the special case of nearest 10, you can use a negative precision:
$new = round($num, -1)

pascal
- 3,287
- 1
- 17
- 35
1
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.

Ignacio Vazquez-Abrams
- 776,304
- 153
- 1,341
- 1,358