PHP number_format() Function
Example
Format a number:
"; echo number_format("1000000",2).""; echo number_format("1000000",2,",","."); ?>
Run Example »
Definition and Usage
The number_format() function formats a number with thousands grouping.
**Note:** This function accepts one, two, or four parameters (not three).
Syntax
number_format(
number,decimals,decimalpoint,separator
)
| Parameters | Description |
|---|---|
| number | Required. The number to format. If no other parameters are set, the number is formatted without a decimal point and uses a comma (,) as the thousands separator. |
| decimals | Optional. Specifies how many decimals. If this parameter is set, a dot (.) is used as the decimal point to format the number. |
| decimalpoint | Optional. Specifies the string used as the decimal point. |
| separator | Optional. Specifies the string used as the thousands separator. Only the first character of this parameter is used. For example "xxx" outputs only "x". |
**注释:**如果设置了该参数,那么所有其他参数都是必需的。 |
Technical Details
| Return Value: | Returns the formatted number. |
|---|---|
| PHP Version: | 4+ |
| Changelog: | Since PHP 5.4, this function supports multibyte in the parameter decimalpoint and separator . In earlier versions, the value used the first byte of each separator. |
More examples
Example 1
You want to return a price: one parameter rounds the number (formatted without decimals), two parameters give you the desired result:
"; echo $formattedNum; $formattedNum = number_format($num, 2); echo $formattedNum; ?>Run Example »
PHP String Reference Manual