en_US international format:
<?php$number = 1234.56;setlocale(LC_MONETARY,"en_US");echo money_format("The price is %i", $number);?>The above code will output:
The price is USD 1,234.56The money_format() function returns a string formatted as a currency string.
This function inserts a formatted number at the percent sign (%) position of the main string.
Note: The money_format() function does not work on Windows platforms.
Tip: This function is often used together with the setlocale() function.
Tip: To see all available language codes, visit our Language Codes Reference.
money_format( string,number )
parameter | describe |
---|---|
string | Required. Specifies the string to be formatted and how to format the variables in the string. Possible format values: Padding and flags: = f - Specifies that the character (f) is used as padding (for example: %=t uses "t" as padding). By default, spaces are used as padding. ^ - Removes the use of grouping characters. + or ( - specifies how to display positive and negative numbers. If "+" is used, the locally set + and - are used (usually a sign is added before negative numbers, and no sign is added before the gift book). If "(" is used, negative numbers Enclosed within parentheses, the default is "+". ! - Stop using currency symbols in output strings. - If "-" is used, all fields are left aligned. Default is right aligned. Field width: x - specifies the minimum width (x) of the field. The 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 is greater than x, this provision will be 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. By default, local settings are used. Convert characters: i - The number is formatted in international currency format. n - The number is formatted in the national currency format. % - Returns the % character. Note: If multiple above format values are used, they must be used in the order above and cannot be disrupted. Note: This function is affected by local settings. |
number | Required. The number inserted into the formatted string at the % sign position. |
Return value: | Returns a formatted string. Characters before and after the format string will be returned unchanged. Non-numeric numbers return NULL and generate E_WARNING. |
---|---|
PHP version: | 4.3.0+ |
International format with 2 decimals (Germany):
<?php$number = 1234.56;setlocale(LC_MONETARY,"de_DE");echo money_format("%.2n", $number);?>The above code will output:
1 234,56 EURNegative numbers, with () indicating the US international format for negative numbers, with a right-hand precision of 2 and "*" as a fill character:
<?php$number = -1234.5672;echo money_format("%=*(#10.2n",$number);?>The above code will output:
(******1234.57)