Open In App

PHP money_format() Function

Last Updated : 14 Jul, 2020
Improve
Improve
Like Article
Like
Save
Share
Report

The money_format() function is an inbuilt function in PHP that returns a number formatted as a currency string. In the main string, the formatted number is inserted where there is a percentage sign (%).
The function is only defined in systems with strfmon() capacities. For example, money_format() is not defined in windows. It is mostly used with another pre-defined PHP function setlocale() with LC_MONETARY as its category of locale settings. As of version 7.4, money_format() has been deprecated. Instead, NumberFormatter::formatCurrency() is used.

Syntax:

string money_format( string str, float num )

Example:

 
money_format("you have to pay %i", $num);

Return value: It returns the formatted string. It will return the unchanged characters before and after the formatting string.

Parameters:

str: It specifies the string to be formatted and the way the variables in it will be formatted. The string parameter consists of the next sequence.

  1. a % character To insert the formatted number in the main string.
  2. optional flags One or more subsequent can be used.
    • =f Numeric fill character. The default fill character is whitespace.
    • ^ Disables the utilization of grouping characters.
    • + or Specifies the formatting style for positive and negative numbers. If + is employed, the locale’s equivalent for + and – will be used. Negative amounts are enclosed in parenthesis. By default the characteristic is + unless the sign of the number is mentioned.(Refer example 2)
    • ! Suppresses the currency symbol out of the output string.
    • If it is present, it makes all the fields left-justified (padded to the right), unlike the default, as per which the fields are right-justified (padded to the left).
  3. width
    • w A decimal digit string that specifies the minimum field width which will be right-
      justified unless the flag is used. Its default value is 0 (zero).
  4. optional left precision
    • #n This is used to define the scale (greatest number of digits (n) to the left of the decimal) of the number. It is used to keep formatted output aligned in the same columns by the use of fill character if the number of digits is less than n. This specification is ignored if the number of digits are greater than n. If ^ flag has not been used, grouping separators will be inserted before the fill characters (if any) are added. If the fill character is a digit, the grouping separators will not be applied to fill characters. Any characters appearing before or after the number in the formatted output are padded as required with the space characters to make sure proper alignment.
  5. optional right precision
    • .p A period is followed after the decimal character by the other (p) number of digits. If p is 0, the decimal character and the digits to its right will be omitted. If no right precision is mentioned, the default will be dictated by the current locale in use. Before the process of formatting, the amount being formatted is rounded to the specified number of digits.
  6. A required conversion character
    • i Formats the number according to the locales international currency format.
    • n Formats the number according to the locales national currency format.
    • % Returns % character.

Number: The number to be formatted.

Example 1: This example prints a given number in its locale international and national format.




<?php
  
$num = 8456.22;
  
setlocale(LC_MONETARY, "en_US");
  
echo money_format("The output in locales"
    . " international format is %i", $num);
echo "\n";
  
echo money_format("The output in locales"
    . " national format is %n", $num);
?>


Output:

The output in locales international format is USD 8, 456.22 
The output in locales national format is $8, 456.22

Example 2: Program that takes a negative number and displays it as a currency.




<?php
  
$num = -8456.22;
setlocale(LC_MONETARY, "en_US");
echo money_format("output: %(n", $num);
?>


Output:

Output: ($8, 456.22)


Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads