问题
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).' €';
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)." €";
来源:https://stackoverflow.com/questions/9194772/php-number-format-decimal-99-is-turned-to-00