开发者

PHP change 0.1 to 0.10

Is there a PHP function that will make the number always hav开发者_如何转开发e 2 decimals places, even if it's 0?


No, but you can format a number to a string with decimals

  • number_format — Format a number with grouped thousands

Example:

echo number_format(0, 2); // 0.00

EDIT: the printf/sprintf solutions suggested deserve some upvotes too


Do you mean you want to print additional digits even if there's only one digit after the decimal? You can do something like:

$x = 0.1;
printf("%.2f", $x);
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜