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

2019-08-23 03:06发布

问题:

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;";