PHP money_format() function
Example
en_US international format:
<?php $number = 1234.56; setlocale(LC_MONETARY,"en_US"); echo money_format("The price is %i", $number); ?>
The output of the above code:
The price is USD 1,234.56
Definition and usage
The money_format() function returns the string formatted as a currency string.
This function inserts a formatted number at the percentage (%) position in the main string.
Note:The money_format() function does not work on the Windows platform.
Tip:This function is often used with setlocale() Functions can be used together.
Tip:To view all available language codes, please visit ourLanguage code reference manual.
Syntax
money_format(string,number)
Parameter | Description |
---|---|
string |
Required. Specifies the string to be formatted and how to format the variables within it. Possible format values: Padding and symbols:
Field width:
Conversion characters:
Note:If multiple format values are used, they must appear in the order listed above. Note:This function is affected by the local settings. |
number | Required. The number inserted into the format string at the position of the % symbol. |
Technical Details
Return value: |
Returns the formatted string. Characters before and after the formatted string will remain unchanged. Non-numeric numbers will return NULL and produce an E_WARNING. |
PHP Version: | 4.3.0+ |
More Examples
Example 1
International format with 2 decimal places (Germany):
<?php $number = 1234.56; setlocale(LC_MONETARY,"de_DE"); echo money_format("%.2n", $number); ?>
The output of the above code:
1 234,56 EUR
Example 2
Negative numbers, with parentheses () indicating negative numbers in the US international format, right precision of 2, and "*" as the fill character:
<?php $number = -1234.5672; echo money_format("%=*(#10.2n",$number); ?>
The output of the above code:
(******1234.57)