PHP money_format()Functions

PHP String 参考手册PHP String Reference Manual

Examples

en_US international format:

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

The code above will output:

The price is USD 1,234.56


Definition and Usage

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

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

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

Tip:This function is often used withsetlocale()function.

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 in the string.

Possible format values:

Padding and flags:

  • =f- Specifies the character (f) to be used as padding (e.g., %=t uses "t" as padding). By default, spaces are used as padding.
  • ^ - Removes the use of grouping characters.
  • + or ( - Specifies how positive and negative numbers are displayed. If "+" is used, the locale's + and - are used (usually a sign before negative numbers, no sign before positive numbers). If "(" is used, negative numbers are enclosed in parentheses. The default is "+".
  • ! - Stops the use of the currency symbol in the output string.
  • - If "-" is used, all fields are left-aligned. By default, they are right-aligned.

Field width:

  • x- Specifies the minimum field width (x). Default is 0.
  • #x- Specifies the maximum number of digits to the left of the decimal point (x). Used to keep formatted output aligned in the same column. If the number of digits is greater than x, this setting is 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 its right will not be displayed. The locale setting is used by default.

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 of the above format values are used, they must be used in the order above and cannot be reordered.

Note:This function is affected by locale settings.

number Required. The number to be inserted at the % sign position in the formatted string.

Technical Details

Return value: Returns the formatted string. Characters before and after the formatted string are returned unchanged. Non-numeric values return NULL and generate an E_WARNING.
PHP Version: 4.3.0+


More Examples

Example 1

International format with 2 decimals (Germany):

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

The code above will output:

1 234,56 EUR


Example 2

Negative number, US international format with () indicating negative numbers, precision of 2 on the right side, "*" as padding character:

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

The code above will output:

(******1234.57)



PHP String 参考手册PHP String Reference Manual Other Extensions