PHP money_format()Functions
Examples
en_US international format:
$number = 1234.56;
setlocale(LC_MONETARY,"en_US");
echo money_format("The price is %i", $number);
?>
The code above will output:
Definition and Usage
The money_format() function returns a string formatted as a currency string.
This function inserts a formatted number in the main string at the position of the percent sign (%).
Note:The money_format() function does not work on Windows platforms.
Tip:This function is often used withsetlocale()function.
Tip:To view all available language codes, please visit ourLanguage Code Reference Manual。
Syntax
| Parameter | Description |
|---|---|
| string | Required. Specifies the string to be formatted and how to format the variables in the string. Possible format values: Padding and flags:
Field width:
Conversion characters:
Note:If multiple of the above format values are used, they must be used in the order above and cannot be reordered. Note:This function is affected by locale settings. |
| number | Required. The number to be inserted 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 generate an E_WARNING. |
|---|---|
| PHP Version: | 4.3.0+ |
More Examples
Example 1
International format with 2 decimals (Germany):
$number = 1234.56;
setlocale(LC_MONETARY,"de_DE");
echo money_format("%.2n", $number);
?>
The code above will output:
Example 2
Negative number, US international format with () indicating negative numbers, precision of 2 on the right side, "*" as padding character:
$number = -1234.5672;
echo money_format("%=*(#10.2n",$number);
?>
The code above will output:
PHP String Reference Manual Other Extensions