开发者

how to display number in a certain format in php [duplicate]

This question already has answers here: Print numeric values to two decimal places (6 answers) Closed 11 months ago.

I'd like to display a number, integer or float in a uniform way like this开发者_开发百科:

if it is integer, for example 10, it should be displayed as 10.00 if it is a float already, for example 10.1, it should be displayed as 10.10 and if the float look like this 10.116, just display the first two number after the dot regardless the left digits following the third one.


$formatted = sprintf("%.2f", $your_number);

Alternatively, if you just want to print it out, and don't need it in a variable:

printf("%.2f", $your_number);


refer number_format

$x=number_format(10, 2);


check number_format()

echo number_format(10, 2); // will output 10.00


You can use php's in built number_format() function. You can get more information from php.net.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜