1
round to nearest
CGuild1.com > Tips & Tricks > round to nearest

Round To The Nearest Number Function - PHP

Here's how to round to the nearest whatever number. It is a function that can be reused.

function nearest($num, $divisor) {
  $diff = $num % $divisor;
  if ($diff == 0)
    return $num;
  elseif ($diff >= ceil($divisor / 2))
    return $num - $diff + $divisor;
  else
    return $num - $diff;
}
Call it like this: <?php echo nearest(23647, 5000);?>

Original source on Stackoverflow
©2024 CGuild1.com