PHP to round up to the 2nd decimal place

前端 未结 2 1638
甜味超标
甜味超标 2021-01-15 06:04

By calculating areas I have a number which I need to display in a strange way.

Always display 2 decimal places. Always round up the 2nd decimal place if the 3rd+ de

相关标签:
2条回答
  • 2021-01-15 06:34

    To always round up you will want to use something like this:

    $number = 0.8701;
    
    echo ceil($number*100)/100;
    
    // = 0.88
    
    0 讨论(0)
  • 2021-01-15 06:36

    You can use 2 functions:

    • round() - docs here: http://www.php.net/manual/en/function.round.php
    • number_format() - docs here: http://ro1.php.net/number_format

    I've used both with success, and depending on what you're doing with the result, you may chose either of the above functions.

    Later edit: If you want to only round up, you can use ceil() - http://www.php.net/manual/en/function.ceil.php + number format or round

    echo round(ceil($number*100)/100,2);
    

    As another user suggested earlier

    0 讨论(0)
提交回复
热议问题