如何在PHP中将数字从621编译到700?

如何在PHP中将数字从621编译到700?

问题描述:

$num=621;
echo round(round(621,-2));
  $ num = 621; 
echo round(round(621,-2)); 
   pre> 
  div>

I think you are trying to round off a number to the nearest 100. Simply do this by using the ceil function.

ceil(621 / 100) * 100;

You can use ceil function to round any number to its nearest number.

$number = ceil($inputNumber / $nearestNumber) * $nearestNumber;

Something like this (returns the closest $multiple not less than the given $number):

function round_up($num, $mul) {
    return ceil($num / $mul) * $mul;
}

Called like this:

echo round_up(621, 100);
> 700

Works for "weird" quantities, too:

echo round_up(124.53, 0.25);
> 124.75

echo round_up(pi(), 1/7);
> 3.1428571428571

If you want to specify decimal places instead of multiples, you could use the power operator ** to convert decimal places to multiples.

You could do round_down in a similar way using floor.

As an option, subtract the reminder and add 100:

function ceil100($value) {
    return $value - $value % 100 + 100;
}

My answer is $numero = $numero + 100 - $numero % 100;

php > $numero = 621;
php > $numero = $numero + 100 - $numero % 100;
php > echo $numero;
700