php保留两位小数的三种方法
更新:HHH   时间:2023-1-7


  1. around:
    a.代码:
    <?php
    $num1 = round(5,2);
    $num2 = round(5.123,2);
    echo $num1.PHP_EOL;
    echo $num2.PHP_EOL;

    b.输出:

    5
    5.12
  2. sprintf:
    a.代码:
    <?php
    $num1 = sprintf('%.2f',5);
    $num2 = sprintf('%.2f',5.123);
    echo $num1.PHP_EOL;
    echo $num2.PHP_EOL;

    b.输出:

    5.00
    5.12
  3. number_format(推荐):
    a.代码:
    <?php
    $num1 = number_format(5,2);
    $num2 = number_format(5.123,2);
    echo $num1.PHP_EOL;
    echo $num2.PHP_EOL;

    b.输出:

    5.00
    5.12
返回web开发教程...