How to Round Up a Number to Nearest 5 or 10 in PHP

Before going into solution we should be familiar with 3 round up functions PHP provides.

  • floor() rounds a number DOWN to the nearest integer if necessary.
  • ceil()  rounds a number UP to the nearest integer if necessary.
  • round() rounds a number to specified precision (number of digits after the decimal point). The precision can also be negative or zero (default). There are 4 rounding modes: PHP_ROUND_HALF_UP, PHP_ROUND_HALF_DOWN, PHP_ROUND_HALF_EVEN, or PHP_ROUND_HALF_ODD.

Round up to nearest 5

$roundedNumber = (ceil($source)%10 === 0) ? ceil($source) : ceil($source/5)*5;
$roundedNumber = (floor($source)%10 === 0) ? floor($source) : floor($source/5)*5;

Round up to nearest 10

$roundedNumber = ceil($source / 10) * 10;
$roundedNumber = floor($source / 10) * 10;
$roundedNumber = round($source, -1, PHP_ROUND_HALF_UP);
$roundedNumber = round($source, -1, PHP_ROUND_HALF_DOWN);

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top

By continuing to use the site, you agree to the use of cookies. more information

The cookie settings on this website are set to "allow cookies" to give you the best browsing experience possible. If you continue to use this website without changing your cookie settings or you click "Accept" below then you are consenting to this.