在PHP中取整到最接近的五的倍数

我想要一个PHP函数返回55时调用它52。

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

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

它返回90,但我想95

谢谢。

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

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; } 
  1. 除以5
  2. round() (或ceil() 如果你总想总结
  3. 乘以5。

值5(分辨率/粒度)可以是任何东西 – 在第1步和第3步中都将其replace

回合:

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

围捕:

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

向最近(向上或向下)回合:

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

试试我写的这个小函数。

 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); 
  echo $value - ($value % 5); 

我知道这是一个古老的问题,但恕我直言,使用模数运算符是最好的方法,并远远优于接受的答案。

乘以2,乘以-1,除以2。

这是我的版本的Musthafa的function。 这一个更复杂,但它支持浮点数和整数。 要舍入的数字也可以是一个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; } } 

我尝试了Knight , Musthafa的function甚至Praesagus的build议。 他们不支持Float数字, Musthafa & Praesagus的解决scheme在某些数字中不能正常工作。 尝试下面的testing数字,自己做比较:

 $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 
 function round_up($n, $x = 5) { $rem = $n % $x; if ($rem < 3) return $n - $rem; else return $n - $rem + $x; } 

我在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); } 

如果有任何问题,请随意修改并修复