E
E
entermix2016-11-15 23:55:22
PHP
entermix, 2016-11-15 23:55:22

How to round a number only up?

There is an array of numbers, for example: 21749, 21751, you need to get 21750, 21800 i.e. round only up.

echo round(21749, -1); // 21750
echo round(21751, -2); // 21800

But what if there are many numbers? Do not set precision manually. did like this:
$numbers = array(21749, 21751,);

foreach ($numbers as $number) {
  $precision  = -1;
  do{
    $r = round($number, $precision);
    $precision += (-1);
  } while($r <  $number);
  echo ':' . $r . '<br/>';
}

Are there other ways to "feng shui"? Those. to round up without specifying the number of characters?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
H
Hello1, 2016-11-16
@entermix

function up(num) {
  return Math.ceil(num/50)*50
}
up(21749); //21750
up(21750); //21750
up(21751); //21800

R
Rsa97, 2016-11-16
@Rsa97

21749 should be rounded up to how many characters? You can get 21750, 21800, 22000, 30000, 100000, 1000000, ... How should the program determine where to stop?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question