【问题标题】:Format numbers correctly in php在php中正确格式化数字
【发布时间】:2014-03-24 01:15:01
【问题描述】:

我正在尝试在 php 中将 2176 格式化为 21.76

我有这个代码:$Payment->amount equals 2176 in this example.

$<?php echo number_format($Payment->amount,'2')/100; ?>

我收到0.02 为什么?

【问题讨论】:

    标签: php number-formatting


    【解决方案1】:

    number_format($Payment-&gt;amount,'2') 给你字符串'2',然后你将它除以100,所以结果是0.02

    应该是:

    $<?php echo number_format($Payment->amount / 100, 2); ?>
    

    【讨论】:

      【解决方案2】:

      您需要在格式化数字之前进行除法

      $<?php echo number_format($Payment->amount/100,'2'); ?>
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2016-08-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2023-04-04
        • 1970-01-01
        相关资源
        最近更新 更多