函数-在PHP中四舍五入到最接近的5的倍数

我想要一个用52调用时返回55的php函数。

我已经尝试了round()函数:

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

返回90,但我要95。

谢谢。

ahmadbrkat asked 2019-11-07T10:51:59Z
12个解决方案
139 votes

这可以通过多种方式来完成,具体取决于您的首选舍入约定:

1.四舍五入到5的下一个倍数,不包括当前数字

行为:50个输出55,52个输出55

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

2.四舍五入到最接近的5的倍数,包括当前数字

行为:50个输出50、52个输出55、50.25个输出50

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

3.舍入到一个整数,然后四舍五入到最接近的5的倍数

行为:50个输出50、52个输出55、50.25个输出55

function roundUpToAny($n,$x=5) {
    return (ceil($n)%$x === 0) ? ceil($n) : round(($n+$x/2)/$x)*$x;
}
SW4 answered 2019-11-07T10:52:52Z
71 votes
  1. 除以5
  2. round()(如果希望始终取整,则为ceil()
  3. 乘以5。

值5(分辨率/粒度)可以是任何值-在步骤1和3中都将其替换

jensgram answered 2019-11-07T10:53:40Z
59 votes

四舍五入:

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

围捕:

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

四舍五入到最接近(向上或向下):

$x = round($x/5) * 5;
PaulJWilliams answered 2019-11-07T10:54:17Z
3 votes

试试我写的这个小功能。

function ceilFive($number) {
    $div = floor($number / 5);
    $mod = $number % 5;

    If ($mod > 0) $add = 5;
    Else $add = 0;

    return $div * 5 + $add;
}

echo ceilFive(52);
Knight answered 2019-11-07T10:54:44Z
3 votes
   echo $value - ($value % 5);

我知道这是一个古老的问题,但是使用模运算符的恕我直言是最好的方法,并且比公认的答案要优雅得多。

Mahn answered 2019-11-07T10:55:11Z
3 votes

从Gears库

MathType::roundStep(50, 5); // 50
MathType::roundStep(52, 5); // 50
MathType::roundStep(53, 5); // 55

MathType::floorStep(50, 5); // 50
MathType::floorStep(52, 5); // 50
MathType::floorStep(53, 5); // 50

MathType::ceilStep(50, 5); // 50
MathType::ceilStep(52, 5); // 55
MathType::ceilStep(53, 5); // 55

资源:

public static function roundStep($value, int $step = 1)
{
    return round($value / $step) * $step;
}

public static function floorStep($value, int $step = 1)
{
    return floor($value / $step) * $step;
}

public static function ceilStep($value, int $step = 1)
{
    return ceil($value / $step) * $step;
}
Cosmologist answered 2019-11-07T10:55:38Z
1 votes

乘以2,四舍五入为-1,除以2。

Ignacio Vazquez-Abrams answered 2019-11-07T10:56:02Z
1 votes

这是我的Musthafa函数的版本。 这个比较复杂,但是它支持浮点数和整数。 要四舍五入的数字也可以在字符串中。

/**
 * @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;
  }
}

我尝试了Knight,Musthafa的功能,甚至尝试过Praesagus的建议。 他们不支持Float数字,并且Musthafa和Praesagus的解决方案在某些数字上无法正常工作。 尝试以下测试编号,然后自己进行比较:

$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
DrupalFever answered 2019-11-07T10:56:33Z
1 votes

我这样做是这样的:

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

测试:

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);
Vaci answered 2019-11-07T10:56:55Z
0 votes
function round_up($n, $x = 5) {
  $rem = $n % $x;
  if ($rem < 3)
     return $n - $rem;
  else
     return $n - $rem + $x;
}
Musthafa answered 2019-11-07T10:57:13Z
0 votes

我只是在20分钟内编写了此函数,根据我在各处发现的许多结果,我不知道它为什么起作用或如何起作用! :D

我主要对将货币数字从151431.1 LBP转换为150000.0 LBP感兴趣。 (151431.1 LBP ==〜100 USD)到目前为止效果很好,但是我试图使其与其他货币和数字兼容,但是不确定是否可以正常使用!

/**
 * Example:
 * Input = 151431.1 >> return = 150000.0
 * Input = 17204.13 >> return = 17000.0
 * Input = 2358.533 >> return = 2350.0
 * Input = 129.2421 >> return = 125.0
 * Input = 12.16434 >> return = 10.0
 *
 * @param     $value
 * @param int $modBase
 *
 * @return  float
 */
private function currenciesBeautifier($value, int $modBase = 5)
{
    // round the value to the nearest
    $roundedValue = round($value);

    // count the number of digits before the dot
    $count = strlen((int)str_replace('.', '', $roundedValue));

    // remove 3 to get how many zeros to add the mod base
    $numberOfZeros = $count - 3;

    // add the zeros to the mod base
    $mod = str_pad($modBase, $numberOfZeros + 1, '0', STR_PAD_RIGHT);

    // do the magic
    return $roundedValue - ($roundedValue % $mod);
}

如有任何错误,请随时进行修改和修复

Mahmoud Zalt answered 2019-11-07T10:57:52Z
0 votes

也许您也可以考虑使用这种衬板。 更快! 适用于$num >= 0$factor > 0

$num = 52;
$factor = 55;
$roundedNum = $num + $factor - 1 - ($num + $factor - 1) % $factor;
Abdullah Al Farooq answered 2019-11-07T10:58:18Z
translate from https://stackoverflow.com:/questions/4133859/round-up-to-nearest-multiple-of-five-in-php