简单的 php Math Objec(ABS 方法)- 得到 2 位小数
我对 php 数学对象有一个小困难。 每当我回显总数时,它都会显示 2 位小数,但当数字有 .00 时则不会。 因此 20.00 - 10.00 将显示 10,而 20.00 - 9.99 将显示 10.01
我希望始终显示 2 位小数。
当我搜索互联网时,我找不到将其添加到我的代码中的方法。我认为应该用 'number_format($total_sub,2)' 来完成,但是怎么做呢?
请参阅解析结束编号的示例代码:
<?php
$less_subtotal= '10.00';
$order_subtotal= '20.00';
$total_sub = abs($order_subtotal - $less_subtotal);
number_format($total_sub, 2);// output is still witouth 2 decimals...
echo $total_sub;
?>
--- 编辑 --- 正如我所描述的那样,我知道问题所在......史诗......
echo number_format($total_sub, 2);
现在应该没问题了吧?
I have a small difficulty with the php math object.
Whenever I echo a total number it will display okay with 2 decimals, except when the number has .00 it will not.
So 20.00 - 10.00 will display 10 while 20.00 - 9.99 will display 10.01
I would like to always show 2 decimals.
As I search the internet I cannot find the way to add it to my code. I think it should be done with a 'number_format($total_sub,2)' , but how?
See example code that parses the end number:
<?php
$less_subtotal= '10.00';
$order_subtotal= '20.00';
$total_sub = abs($order_subtotal - $less_subtotal);
number_format($total_sub, 2);// output is still witouth 2 decimals...
echo $total_sub;
?>
--- EDIT ---
As I described the posted I knew the problem.... EPIC..
echo number_format($total_sub, 2);
Should be okay now I guess?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您没有将
number_format
的结果分配给任何变量。将其重新分配给变量You did not assigned the result of
number_format
to any variable. Assign back it to the variable我刚刚测试了这个,它工作正常:
I just tested this and it works fine: