how to display number in a certain format in php [duplicate]
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.
精彩评论