The money_format() function wraps the C library function strfmon(), with the difference that this implementation converts only one number simultaneously.
PHP money_format() is a built-in function that returns the string formatted as the currency string. The money_format() function inserts a formatted number where there is a percent (%) sign in the main string.
PHP money_format() function is a predefined function that uses a format number as a currency string. It returns a formatted version of the number. The money_format() function does not work on the Windows platforms.
The money_format() function returns a formatted version of the number.
See the following syntax.
The string parameter is required. Specifies a string to be formatted and how to format the variables in it.
Possible format values
Padding and Flags
- =f – Defines the character (f) to be used as padding (Example: %=t. This uses “t” as padding). Default is space
- ^ – Removes the use of grouping characters
- + or ( – Defines how to show positive and negative numbers. If “+” is used, a local setting for + and – will be used (usually the sign in front of the negative numbers and nothing in front of the positive numbers). If “(” is used, the negative numbers are enclosed in the parenthesis. Default is “+”.
- ! – It stops the use of currency symbols in the output string.
- – If “-” is used, all fields are left-justified. The default is right-justified.
- x – Defines the minimum field width (x). Default is 0
- #x – Defines the maximum number (x) of digits expected to the left of the decimal point. It is used to keep formatted output aligned in the same columns. If the number of digits is more significant than x, this specification is ignored
- .x – Defines the maximum number (x) of digits expected to the right of the decimal point. If x is 0, then the decimal point and the digits to its right will not be shown. The default is local settings.
- i – The number is formatted to an international currency format.
- n – The number is formatted to a national currency format.
- % – It returns the % character.
If multiple format values are used, they must be in the same order as shown above. This function is affected by local settings.
The number parameter is required, and the number is inserted at the %-sign in the format string.
See the following code example.
<?php $number = 1921.11; setlocale(LC_MONETARY,"de_DE"); echo money_format("%.2n", $number);
See the output.
➜ pro php app.php Eu1.921,11 ➜ pro
money_format for US Dollar
Let’s see for the USD.
<?php $number = 1921.11; setlocale(LC_MONETARY, 'en_US.UTF-8'); echo money_format('%.2n', $number);
See the output.
➜ pro php app.php $1,921.11 ➜ pro
If the money_format() function doesn’t seem to be working correctly, make sure you define a valid locale.
Display Currency in Indian Numbering Format In PHP
We can display currency in the Indian numbering format using number_format() in PHP.
<?php $amount = '1000'; setlocale(LC_MONETARY, 'en_IN'); $amount = money_format('%!i', $amount); echo $amount;
The above code will work for you.
Double-check that money_format() is defined on any version of PHP you plan your code to run on. You might be surprised.
That’s it for this tutorial.