开发者

How to add numbers after decimal using php

开发者 https://www.devze.com 2023-01-24 18:01 出处:网络
I am having trouble thinking of a php function to add zeros after a decimal. Let say I have $money=\"100开发者_开发百科00\" I need a function that will add .00 to 10000.00 and to add just a zero to 0

I am having trouble thinking of a php function to add zeros after a decimal. Let say I have $money="100开发者_开发百科00" I need a function that will add .00 to 10000.00 and to add just a zero to 0 after 234.5. can anyone help me please?


Just as Dan Grossman said; number_format is your friend here and you'd accomplish those double zeros by:

$money = "1234";
$formatedNumber = number_format($money, 2, '.', '');
echo $formatedNumber;

// 1234.00


The function is number_format

http://php.net/manual/en/function.number-format.php

Always search the PHP manual first :)


Maybe not the answer you want to hear, but I think you're better off storing these values in numeric types and formatting them properly just before output

0

精彩评论

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