Round large non-decimal numbers, up and down with php

php

Solution

You can use this function:

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:

nearest(23647, 5000);

Similar functions, but when you want to decide for yourself in which direction to round:

function roundUp($num, $divisor) {
  $diff = $num % $divisor;
  if ($diff == 0)
    return $num;
  else
    return $num - $diff + $divisor;
}


function roundDown($num, $divisor) {
  $diff = $num % $divisor;
  return $num - $diff;
}

Problem

How do I round larger numbers in php. NOTE: I have aready tried the round function and can not seem to get it to work as i need for example: Say i have 4 listing in a database and they have 4 different prices. ``` 1st Price = 5,783 2nd Price = 19,647 3rd Price = 12,867 4th Price = 23,647 ``` Now we determin that the lowest price in the databae would be 5,783 and the highest price is 23,647. now what i want to do is round down the lowest price to say the nearest 500 or 1000 or even 5000 example of nearset 1000 lowest price 5,783 rounded down = 5000 highest price 23,647 rounded up = 24000

Original source