2

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

Paul R
  • 208,748
  • 37
  • 389
  • 560
Sohail Anwar
  • 304
  • 1
  • 5
  • 16

3 Answers3

4

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

Dennis Haarbrink
  • 3,738
  • 1
  • 27
  • 54
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