phprounding

How to round up a number to nearest 10?


How can we round off a number to the nearest 10 in php?

Say I have 23, what code would I use to round it off to 30?


Solution

  • floor() will go down.

    ceil() will go up.

    round() will go to nearest by default.

    Divide by 10, do the ceil, then multiply by 10 to reduce the significant digits.

    $number = ceil($input / 10) * 10;
    

    Edit: I've been doing it this way for so long.. but TallGreenTree's answer is cleaner.