PHP money_format() function

Example

en_US international format:

<?php
$number = 1234.56;
setlocale(LC_MONETARY,"en_US");
echo money_format("The price is %i", $number);
?>

The output of the above code:

The price is USD 1,234.56

Definition and usage

The money_format() function returns the string formatted as a currency string.

This function inserts a formatted number at the percentage (%) position in the main string.

Note:The money_format() function does not work on the Windows platform.

Tip:This function is often used with setlocale() Functions can be used together.

Tip:To view all available language codes, please visit ourLanguage code reference manual.

Syntax

money_format(string,number)
Parameter Description
string

Required. Specifies the string to be formatted and how to format the variables within it.

Possible format values:

Padding and symbols:

  • =f - Specifies the character (f) to be used as padding (for example: %=t uses "t" as padding). The default padding character is a space.
  • ^ - Removes the use of grouping characters.
  • + or ( - Specifies how positive and negative numbers are displayed. If "+" is used, the local settings of + and - are used (usually a sign is added before negative numbers, and no sign is added before positive numbers). If "(" is used, negative numbers are enclosed in parentheses. The default is to use "+".
  • ! - Stops the use of the currency symbol in the output string.
  • - If "-" is used, all fields are left-aligned. The default is right-aligned.

Field width:

  • x - Specifies the minimum width of the field (x). The default is 0.
  • #x - Specifies the maximum number of digits on the left side of the decimal point (x). It is used to keep the formatted output aligned in the same column. If the number of digits exceeds x, this specification will be ignored.
  • .x - Specifies the maximum number of digits to the right of the decimal point (x). If x is 0, the decimal point and the digits to the right will not be displayed. The default is the local setting.

Conversion characters:

  • i - The number is formatted as an international currency format.
  • n - The number is formatted as a national currency format.
  • % - Returns the % character.

Note:If multiple format values are used, they must appear in the order listed above.

Note:This function is affected by the local settings.

number Required. The number inserted into the format string at the position of the % symbol.

Technical Details

Return value:

Returns the formatted string.

Characters before and after the formatted string will remain unchanged. Non-numeric numbers will return NULL and produce an E_WARNING.

PHP Version: 4.3.0+

More Examples

Example 1

International format with 2 decimal places (Germany):

<?php
$number = 1234.56;
setlocale(LC_MONETARY,"de_DE");
echo money_format("%.2n", $number);
?>

The output of the above code:

1 234,56 EUR

Example 2

Negative numbers, with parentheses () indicating negative numbers in the US international format, right precision of 2, and "*" as the fill character:

<?php
$number = -1234.5672;
echo money_format("%=*(#10.2n",$number);
?>

The output of the above code:

(******1234.57)