开发者

PHP change 0.1 to 0.10

开发者 https://www.devze.com 2023-01-19 11:22 出处:网络
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

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

精彩评论

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