Round to the nearest multiple of five in PHP

I need a php function that returns 55 when calling 52.

I tried the round() function:

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

It returns 90, but I want 95.

Thank.

+48
function php rounding
Nov 09 '10 at
source share
9 answers

This can be done in several ways, depending on your preferred rounding agreement:

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

Behavior: 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 includes the current number

Behavior: 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 to an integer, then to the nearest multiple of 5

Behavior: 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; } 
+101
Nov 09 '10 at 13:00
source share
  • Divided by 5
  • round() (or ceil() if you want to always round)
  • Multiply by 5.

The value 5 (resolution / granularity) can be any; replaced it in both stages 1 and 3

+51
Nov 09 '10 at 12:59
source share

Round down:

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

Round up:

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

Nearest (up or down):

 $x = round($x/5) * 5; 
+35
Nov 09 '10 at 13:00
source share

Try this little function that I wrote.

 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); 
+3
Nov 09 '10 at 13:08
source share
  echo $value - ($value % 5); 

I know this is an old question, but IMHO using the module operator is a better way and much more elegant than the accepted answer.

+3
Jul 25 2018-12-12T00:
source share

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

+1
Nov 09 '10 at
source share

Here is my version of the Musthafa feature . This is more complicated, but 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 of Knight , Musthafa and even a suggestion from Praesagus . They have no support for Float numbers, and solutions from Musthafa's and 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 
+1
Apr 29 '14 at 19:10
source share
 function round_up($n, $x = 5) { $rem = $n % $x; if ($rem < 3) return $n - $rem; else return $n - $rem + $x; } 
0
Sep 18 '12 at 8:27
source share

I just wrote this function in 20 minutes, based on many results that I found here and there, I don’t know why it works or how it works !: D

I was mainly interested in converting currency numbers from this 151431.1 LBP to 150,000.0 LBP. (151431.1 LBP == ~ 100 USD), which works fine so far, however I tried to make it somehow compatible with other currencies and numbers, but not sure if it works fine !!

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

Feel free to change it and fix it if something is wrong.

0
Sep 23 '16 at 16:25
source share



All Articles