开发者

How to add zeros onto a number

I have a 开发者_运维技巧number that is being returned from my database and I'd like to have trailing zeros after the decimal place. I've tried round but that doesn't work.

$18.0 should be $18.00. What function would I use for this?


You can use number_format function for that.

Or sprintf like this:

$money1 = 68.75;
$money2 = 54.35;
$money = $money1 + $money2;

// echo $money will output "123.1";

$formatted = sprintf("%01.2f", $money); // << this does the trick!

// echo $formatted will output "123.10"


printf\sprintf depending on your db it may be easier to use it than php


Try money_format():

$number = 1234.56;

setlocale(LC_MONETARY, 'en_US');
echo money_format('%i', $number) . "\n";

Complete information on money_format is here: http://php.net/manual/en/function.money-format.php


Look at the documentation on sprintf.

sprintf("%.2f",$var)
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜