PHP number_format() 将小数 0.99 转换为 0.00

发布于 2025-01-03 05:47:22 字数 222 浏览 5 评论 0原文

我想把一个“肮脏”的数字变成一个正确书写的货币价格。 输入数字类似于 23.99000,我想显示 23,99 + 欧元符号。

我使用这个:

$price = number_format($price, 2, ',', '')." €";

但结果将是 23,00 € 而不是 23,99 €。

我错了什么?谢谢!

I want to turn a "dirty" figure into a properly written currency price.
The input figure will be something like 23.99000 and I want to display 23,99 + the Euro symbol.

I use this:

$price = number_format($price, 2, ',', '')." €";

But the result will be 23,00 € instead of 23,99 €.

What am I getting wrong? Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

御守 2025-01-10 05:47:22

尝试如下操作:

<?php
$price = 23.99000;
$price = sprintf("%01,2f", $price).' €';
echo $price;

输出:

23,99 €

f 表示它将把字符串视为浮点数,并相应地设置格式。

Try something like this:

<?php
$price = 23.99000;
$price = sprintf("%01,2f", $price).' €';
echo $price;

Output:

23,99 €

the f means it'll treat the string as a float, and format accordingly.

望她远 2025-01-10 05:47:22

为什么不简单地使用 round ?

$price = round($price, 2)." €";

Why not simply using round ?

$price = round($price, 2)." €";
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文