How to round up a number to nearest 10?

tarnfeld picture tarnfeld · Oct 24, 2009 · Viewed 107.2k times · Source

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?

Answer

Daren Schwenke picture Daren Schwenke · Oct 25, 2009

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.