如何在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 $mul
tiple not less than the given $num
ber):
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