I want to format a float value I have to two decimal places.
我想要格式化一个浮点数,我要小数点后两位。
For example if I have a float for a price of 5.2
, I want it to be formatted as 5.20
.
例如,如果我有一个5.2的浮动,我希望它被格式化为5.20。
4 个解决方案
#2
3
You'll want to use printf
or sprintf
. You can read more about it in the php docs.
您需要使用printf或sprintf。您可以在php文档中了解更多。
#3
2
If you're simply trying to ensure a number is displayed with two decimal places, you could also use number_format, or (if this is a currency value) money_format.
如果您只是想确保数字显示为两个小数,您还可以使用number_format,或者(如果是货币值)money_format。
e.g.: echo number_format('5.2', 2);
例如:回声number_format(“5.2”,2);
#4
-3
Try this:
试试这个:
number_format((float)$foo, 2, '.', '');
#1
#2
3
You'll want to use printf
or sprintf
. You can read more about it in the php docs.
您需要使用printf或sprintf。您可以在php文档中了解更多。
#3
2
If you're simply trying to ensure a number is displayed with two decimal places, you could also use number_format, or (if this is a currency value) money_format.
如果您只是想确保数字显示为两个小数,您还可以使用number_format,或者(如果是货币值)money_format。
e.g.: echo number_format('5.2', 2);
例如:回声number_format(“5.2”,2);
#4
-3
Try this:
试试这个:
number_format((float)$foo, 2, '.', '');