【问题标题】:How do you get number_format() to add two decimal points on integers less than 10?你如何让 number_format() 在小于 10 的整数上添加两个小数点?
【发布时间】:2011-05-25 00:21:17
【问题描述】:

价格以美分为单位,但只要价格低于 10 美元,它就不会增加 0.00!

              $price = (float)($cents/100);
              $price = number_format($price,2);   

如果 number_format() 不起作用,我希望能够表示 0.00 和 0.01 和 1.01 不知道该怎么做!

【问题讨论】:

    标签: php decimal number-formatting


    【解决方案1】:

    看看money_format

    <?php
    
    $prices = array(100, 10, 1, 0.10, 0.01);
    foreach ($prices as $price) {
        echo money_format('%.2n', $price) . "\n";
    }
    
    // 100.00
    // 10.00
    // 1.00
    // 0.10
    // 0.01
    ?>
    

    【讨论】:

    • 我对 PHP 了解得越多,我就越希望在某个地方有一个 kitchen_sink() 函数。
    猜你喜欢
    • 1970-01-01
    • 2021-01-02
    • 1970-01-01
    • 2016-11-01
    • 2015-05-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-12-26
    相关资源
    最近更新 更多