0

I want to round up any given number to the nearest set of specific values only. The values are 5,10,20,25,50,100

So if the given number is 2 it will be rounding up to 5. If it's 71.6 it will be rounding down to 50, if it's 250 it will be rounding down to 100

Anybody can give me a clue how to do that? Thanks a lot!

zoora
  • 65
  • 2
  • 8
  • Didnt this:: http://stackoverflow.com/questions/12913353/round-integer-to-nearest-multiple-of-5-in-php answer your question.. or you simply didnt search before posting..! – Sudhir Bastakoti Nov 17 '14 at 00:43
  • Hi, ofcourse I did a search before. But I don't want it to be rounding up to multiple of five. I just want it to be rounding up to specific numbers. – zoora Nov 17 '14 at 00:45
  • You're going to have to be a bit more specific then, including an example. Your question is unclear. – Funk Forty Niner Nov 17 '14 at 00:51
  • 1
    @zoora You did? The most common... :) http://stackoverflow.com/questions/6147356/find-closest-number-in-an-array, http://stackoverflow.com/questions/5464919/php-nearest-value-from-an-array – kwarunek Nov 17 '14 at 00:59
  • @Fred-ii- I have improved the question thanks – zoora Nov 17 '14 at 01:03
  • Have a look at the two links above, provided by kAlmAcetA. Those look promising. – Funk Forty Niner Nov 17 '14 at 01:04
  • @kAlmAcetA my bad I was looking for different keyword :( thanks for the direction. it solves my problem – zoora Nov 17 '14 at 01:36

2 Answers2

0

use a loop:

$numbers = array(5, 10, 20, 25, 50, 100);
$result = $input;
foreach ($numbers as $n) {
    if ($input < $n) {
        $result = $n;
        break;
    }
}
Barmar
  • 741,623
  • 53
  • 500
  • 612
0

I found the answer here : Find number which is greater than or equal to N in an array. Thanks to @kAlmAcetA

function closest($array, $number) {

    sort($array);
    foreach ($array as $a) {
        if ($a >= $number) return $a;
    }
    return end($array) // or return NULL;
}
Community
  • 1
  • 1
zoora
  • 65
  • 2
  • 8