简单的 php Math Objec(ABS 方法)- 得到 2 位小数

发布于 12-25 04:35 字数 642 浏览 2 评论 0原文

我对 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

清泪尽2025-01-01 04:35:34
<?php 
$less_subtotal= '10.00';
$order_subtotal= '20.00';
$total_sub = abs($order_subtotal - $less_subtotal);  
echo bcadd($total_sub,0,2);
?>


Just tested...
<?php 
$less_subtotal= '10.00';
$order_subtotal= '20.00';
$total_sub = abs($order_subtotal - $less_subtotal);  
echo bcadd($total_sub,0,2);
?>


Just tested...
如日中天2025-01-01 04:35:34

您没有将 number_format 的结果分配给任何变量。将其重新分配给变量

$total_sub=number_format($total_sub, 2);// output is still witouth 2 decimals...
echo $total_sub;

You did not assigned the result of number_format to any variable. Assign back it to the variable

$total_sub=number_format($total_sub, 2);// output is still witouth 2 decimals...
echo $total_sub;
作业与我同在2025-01-01 04:35:34

我刚刚测试了这个,它工作正常:

$less_subtotal= (float)10.00;
$order_subtotal= (float)20.00;
$total_sub = abs($order_subtotal - $less_subtotal);  
$total_sub = number_format($total_sub, 2);//assign number_format to variable
echo $total_sub;

I just tested this and it works fine:

$less_subtotal= (float)10.00;
$order_subtotal= (float)20.00;
$total_sub = abs($order_subtotal - $less_subtotal);  
$total_sub = number_format($total_sub, 2);//assign number_format to variable
echo $total_sub;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文