Round up to the nearest 50,000 in PHP

前端 未结 3 1410
青春惊慌失措
青春惊慌失措 2021-01-17 00:50

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 fo

3条回答
  •  清酒与你
    2021-01-17 01:12

    /**
     * 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.

提交回复
热议问题