AppDividend
Latest Code Tutorials

PHP money_format() Function Example Tutorial

0

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.

PHP money_format()

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.

money_format(string, number)

String parameter

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

  1. =f – Defines the character (f) to be used as padding (Example: %=t this uses “t” as padding). Default is space
  2. ^ – Removes the use of grouping characters
  3. + 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 “+”.
  4. ! – It stops the use of currency symbols in the output string.
  5. – If “-” is used, all fields are left-justified. Default is right-justified.

Field width

  1. x – Defines the minimum field width (x). Default is 0
  2. #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
  3. .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.

Conversion characters

  1. i – The number is formatted to an international currency format.
  2. n – The number is formatted to a national currency format.
  3. % – 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.

Number Parameter

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.

#Recommended Posts

PHP Array Push Example | array_push() Tutorial

PHP Array Pop Example | PHP array_pop() Function Tutorial

PHP array_chunk Example | array_chunk Function Tutorial

How To Convert PHP Array To JSON Tutorial With Example

PHP Serialize Example | Serialize() Function Tutorial

 

Leave A Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.