How can I make sure a float will always be rounded up with PHP?

前端 未结 5 628
野趣味
野趣味 2020-12-09 08:33

I want to make sure a float in PHP is rounded up if any decimal is present, without worrying about mathematical rounding rules. This function would work as follows:

5条回答
  •  情话喂你
    2020-12-09 08:42

    I like Ash's response, although I would have:

    $fig = (int) str_pad('1', $precision + 1, '0');
    

    Makes sense that if I provide precision '2', I would expect it rounded to 2 decimal places. Matter of choice though I suppose. Thanks for the answer Ash, works well.

提交回复
热议问题