PHP money_format() Function
Example
en_US international format:
The code above will output:
The price is USD 1,234.56
Definition and Usage
The money_format() function returns a string formatted as a currency string.
This function inserts a formatted number at the percent (%) position in the main string.
**Note:** The money_format() function does not work on Windows platforms.
**Tip:** This function is often used together with setlocale() function.
**Tip:** To view all available language codes, visit ourLanguage Code Reference Manual.
Syntax
money_format(
string,number
)
| Parameters | Description |
|---|---|
| string | Required. Specifies the string to format and how its variables are formatted.
Possible format values: Padding and flags: |
Field width:
- x - Specifies the minimum width (x) of the field. Default is 0.
- #x - Specifies the maximum number of digits to the left of the decimal point (x). Used to keep formatted output aligned in the same column. If the number of digits exceeds x, this rule is ignored.
- .x - Specifies the maximum number of digits to the right of the decimal point (x). If x is 0, the decimal point and the digits to its right will not be displayed. Default uses the locale setting.
Conversion characters:
- i - Number is formatted as an international currency.
- n - Number is formatted as a national currency.
- % - Returns the % character.
Note:If multiple of the above format values are used, they must be used in the order shown above and cannot be rearranged.
Note:This function is affected by the locale setting.
| | *number* | Required. The number to insert at the % sign position in the formatted string. |Technical Details
| Return Value: | Returns the formatted string. Characters before and after the formatted string are returned unchanged. Non-numeric values return NULL and produce an E_WARNING. |
|---|---|
| PHP Version: | 4.3.0+ |
More examples
Example 1
International format with 2 decimals (Germany):
The code above will output:
1 234,56 EUR
Example 2
Negative number with () indicating the negative value, US international format, precision 2 on the right, "*" as the padding character:
The code above will output:
(******1234.57)
PHP String Reference Manual