PHP number_format() decimal .99 is turned to .00

余生颓废 提交于 2019-12-12 02:50:05

问题


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!


回答1:


Try something like this:

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

Output:

23,99 €

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




回答2:


Why not simply using round ?

$price = round($price, 2)." &#8364;";


来源:https://stackoverflow.com/questions/9194772/php-number-format-decimal-99-is-turned-to-00

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!