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

2019-08-23 03:07发布

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!

2条回答
ら.Afraid
2楼-- · 2019-08-23 03:23

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.

查看更多
Emotional °昔
3楼-- · 2019-08-23 03:24

Why not simply using round ?

$price = round($price, 2)." &#8364;";
查看更多
登录 后发表回答