PHP money_format() Function Example Tutorial
PHP money_format() Function Example Tutorial is today’s topic. The money_format() function 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 predefined function. It is used formats 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() returns a formatted version of the number. The money_format() function wraps the C library function strfmon(), with a difference that this implementation converts only one number at a time. 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 a 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, then 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. Default is right-justified.
- x – Defines the minimum field width (x). Default is 0
- #x – Defines the maximum number (x) of digits expected to a 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. 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 to be 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 money_format() function doesn’t seem to be working correctly, make sure you are defining a valid locale.
Display Currency in Indian Numbering Format In PHP
We can display currency in 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.
Finally, PHP money_format() Function Example Tutorial is over.