问题
Is there a way to round up to the nearest 50,000 in PHP?
I've investigated round, but the docs don't suggest a way to do this, and it looks as though it is only for rounding up/down to the nearest numner. Thanks.
回答1:
/**
* Round a number up to the nearest multiple of $n.
*
* @param int $int Number to round.
* @param int $n Round to the nearest $n.
*
* @return int
*/
function round_up_to_nearest_n($int, $n) {
return ceil($int / $n) * $n;
}
echo round_up_to_nearest_n(74268, 50000); //Outputs 100000
Divide by the number you want to round against, do the rounding, then multiply by it again.
回答2:
How about this:
$number = ceil($value / 50000) * 50000;
回答3:
Try this:
$number = "78921";
$round = round($number / 50000) * 50000;
来源:https://stackoverflow.com/questions/12745332/round-up-to-the-nearest-50-000-in-php