【问题标题】:Simple substraction on PHP (Arithmetic Operators)PHP中的简单减法(算术运算符)
【发布时间】:2011-08-18 13:31:55
【问题描述】:

我需要一些关于这个简单减法的帮助:

$savings = ($newprice - $newvalue);

$newprice 和 $newvalue 是类似 120.0040.00 的值

问题是结果显示为 80 ,末尾没有 .00 (dot00)。

我希望保持相同格式的值(一个点加上末尾的双 cero);我需要更改或包含哪些代码?

提前致谢

【问题讨论】:

标签: php math subtraction


【解决方案1】:

http://php.net/number_format

$savings = number_format($newprice - $newvalue, 2);

【讨论】:

  • 谢谢,这只是一个小问题,会影响像 40.50 或 40.99 这样的值吗?
【解决方案2】:
$savings = sprintf("%0.2f", ($newprice - $newvalue));

【讨论】:

    【解决方案3】:

    检查这个函数:

    number_format();
    

    【讨论】:

      【解决方案4】:

      我认为你需要这样做

      $savings = 80;
      echo number_format($savings);
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2019-06-02
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-06-24
        • 1970-01-01
        • 2022-01-07
        相关资源
        最近更新 更多