当前位置: 首页 > 网络学院 > 服务端脚本教程 > PHP > money_format()函数
The money_format() function returns a string formatted as a currency string.
money_format()函数的作用是:将数字格式化为货币形式。
This function inserts a formatted number where there is a percent (%) sign in the main string.
通过这个函数,我们将插入一个格式化的数字。在这个函数的定义过程中,我们将在string参数前加上“%”。
money_format(string,number) |
Parameter参数 | Description描述 |
---|---|
string | Required. Specifies the string to be formatted and how to format the variables in it. 必要参数。指定需要被格式化的字符串以及以何种方式去定义它的格式 Possible format values: Padding and Flags:
Field width:
Conversion characters:
Note: If multiple format values are used, they must be in the same order as shown above. Note: This function is affected by local settings. |
number | Required. The number to be inserted at the %-sign in the format string 必要参数。在格式化的字符串前插入%符号 |
Note: The money_format() function does not work on Windows platforms.
注意:Windows操作平台不支持money_format()函数。
International en_US format:
国际通用的en_US格式:
<?php $number = 1234.56; setlocale(LC_MONETARY, "en_US"); echo money_format("The price is %i", $number); ?> |
The output of the code above will be:
上述代码将输出下面的结果:
The price is USD 1,234.56 |
National Norwegian format with 2 decimals:
挪威格式,2位小数:
<?php $number = 1234.56; setlocale(LC_MONETARY, "no_NO"); echo money_format("%.2n", $number); ?> |
The output of the code above will be:
上述代码将输出下面的结果:
NOK 1.234,56 |
Negative number, US national format with () to indicate negative numbers and 2 digits of right precision and "*" as fill character:
负数情况,美国的格式使用()来表示复数,设值为两位小数,并在数字的左边以“*”作为填充字符串:
<?php $number = -1234.5672; echo money_format("%=*(#10.2n", $number); ?> |
The output of the code above will be:
上述代码将输出下面的结果:
($********1,234.57) |