Definition and Usage:
The money_format() function formatted a string as formatted it as currency string.
That function has built in feature as so it called money_formatted, it insert formatted number like($,£) in the string where there is (%) sign in the main string.
Important Note: The money_format() function of php does not work on Windows OS.
Both the parameters string and number are mandatory.
String: Specify the format of the string and how to format it.
Input size of Text Field:
x - Denotes the minimum field width (x). Default is 0
#x - Denotes the maximum number (x) of digits estimated to the left of the decimal point. This is used to keep formatted output aligned in the same columns. If the number of digits is larger than x, this requirement is ignored.
x - Specifies the maximum number (x) of digits estimated to the right of the decimal point. If x is 0, the decimal point and the digits to its right will not be shown.
There are three Conversion characters:
i - International currency format
n - National currency format
% - Returns the % character
If multiple characters are used then it uses it in same order in %.
Padding and Flags:
=f - Specifies the character (f) to be used as padding (Example: %=s this uses "s" as padding). Default is space
International en_US format:
$var = 1345.78;
echo money_format("The price is %i", $var);
The price is USD 1,345.78
National Norwegian format with 2 decimals:
$var = 1345.78;
echo money_format("%.2n", $var);
Negative number, US national format with () to indicate negative numbers and 2 digits of right precision and "&" as fill character:
$var = -1345.5782;
echo money_format("%=& (#10.2n", $var);
Using money_format() function in php