Round up to nearest multiple of five in PHP

2019-01-05 02:53发布

I want a php function which returns 55 when calling it with 52.

I've tried the round() function:

echo round(94, -1); // 90

It returns 90 but I want 95.

Thanks.

12条回答
SAY GOODBYE
2楼-- · 2019-01-05 03:38
   echo $value - ($value % 5);

I know it's an old question, but IMHO using modulus operator is the best way, and far more elegant than the accepted answer.

查看更多
可以哭但决不认输i
3楼-- · 2019-01-05 03:40

Here is my version of Musthafa's function. This one is more complex but it has support for Float numbers as well as Integers. The number to be rounded can also be in a string.

/**
 * @desc This function will round up a number to the nearest rounding number specified.
 * @param $n (Integer || Float) Required -> The original number. Ex. $n = 5.7;
 * @param $x (Integer) Optional -> The nearest number to round up to. The default value is 5. Ex. $x = 3;
 * @return (Integer) The original number rounded up to the nearest rounding number.
 */
function rounduptoany ($n, $x = 5) {

  //If the original number is an integer and is a multiple of 
  //the "nearest rounding number", return it without change.
  if ((intval($n) == $n) && (!is_float(intval($n) / $x))) {

    return intval($n);
  }
  //If the original number is a float or if this integer is 
  //not a multiple of the "nearest rounding number", do the 
  //rounding up.
  else {

    return round(($n + $x / 2) / $x) * $x;
  }
}

I tried the functions from Knight, Musthafa and even the suggestion from Praesagus. They don't have support for Float numbers and the solutions from Musthafa's & Praesagus do not work correctly in some numbers. Try the following test numbers and do the comparison yourself:

$x= 5;

$n= 200;       // D = 200     K = 200     M = 200     P = 205
$n= 205;       // D = 205     K = 205     M = 205     P = 210
$n= 200.50;    // D = 205     K = 200     M = 200.5   P = 205.5
$n= '210.50';  // D = 215     K = 210     M = 210.5   P = 215.5
$n= 201;       // D = 205     K = 205     M = 200     P = 205
$n= 202;       // D = 205     K = 205     M = 200     P = 205
$n= 203;       // D = 205     K = 205     M = 205     P = 205

** D = DrupalFever K = Knight M = Musthafa P = Praesagus
查看更多
霸刀☆藐视天下
4楼-- · 2019-01-05 03:41

Round down:

$x = floor($x/5) * 5;

Round up:

$x = ceil($x/5) * 5;

Round to closest (up or down):

$x = round($x/5) * 5;
查看更多
仙女界的扛把子
5楼-- · 2019-01-05 03:41

Multiply by 2, round to -1, divide by 2.

查看更多
我想做一个坏孩纸
6楼-- · 2019-01-05 03:41

I do it like this:

private function roundUpToAny(int $n, $x = 9)
{
    return (floor($n / 10) * 10) + $x;
}

Tests:

assert($this->roundUpToAny(0, 9) == 9);
assert($this->roundUpToAny(1, 9) == 9);
assert($this->roundUpToAny(2, 9) == 9);
assert($this->roundUpToAny(3, 9) == 9);
assert($this->roundUpToAny(4, 9) == 9);
assert($this->roundUpToAny(5, 9) == 9);
assert($this->roundUpToAny(6, 9) == 9);
assert($this->roundUpToAny(7, 9) == 9);
assert($this->roundUpToAny(8, 9) == 9);
assert($this->roundUpToAny(9, 9) == 9);
assert($this->roundUpToAny(10, 9) == 19);
assert($this->roundUpToAny(11, 9) == 19);
assert($this->roundUpToAny(12, 9) == 19);
assert($this->roundUpToAny(13, 9) == 19);
assert($this->roundUpToAny(14, 9) == 19);
assert($this->roundUpToAny(15, 9) == 19);
assert($this->roundUpToAny(16, 9) == 19);
assert($this->roundUpToAny(17, 9) == 19);
assert($this->roundUpToAny(18, 9) == 19);
assert($this->roundUpToAny(19, 9) == 19);
查看更多
仙女界的扛把子
7楼-- · 2019-01-05 03:44

This can be accomplished in a number of ways, depending on your preferred rounding convention:

1. Round to the next multiple of 5, exclude the current number

Behaviour: 50 outputs 55, 52 outputs 55

function roundUpToAny($n,$x=5) {
    return round(($n+$x/2)/$x)*$x;
}

2. Round to the nearest multiple of 5, include the current number

Behaviour: 50 outputs 50, 52 outputs 55, 50.25 outputs 50

function roundUpToAny($n,$x=5) {
    return (round($n)%$x === 0) ? round($n) : round(($n+$x/2)/$x)*$x;
}

3. Round up to an integer, then to the nearest multiple of 5

Behaviour: 50 outputs 50, 52 outputs 55, 50.25 outputs 55

function roundUpToAny($n,$x=5) {
    return (ceil($n)%$x === 0) ? ceil($n) : round(($n+$x/2)/$x)*$x;
}
查看更多
登录 后发表回答