Latest web development tutorials

PHP money_format () function

PHP String Reference PHP String Reference

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

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

This function inserts a formatted number in the percent main strings (%) position.

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

Tip: This function is often the setlocale () function is used together.

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


grammar

money_format( string,number )

参数 描述
string 必需。规定要格式化的字符串及如何格式化字符串中的变量。

可能的格式值:

填充和标志:

  • =f- 规定字符(f)用作填充(例如:%=t 使用 "t" 作为填充)。默认使用空格作为填充。
  • ^ - 移除分组字符的使用。
  • + 或 ( - 规定如何显示正数和负数。如果使用 "+",则使用本地设置的 + 和 -(通常在负数前加符号,赠书前不加任何符号)。如果使用 "(",负数被包含在括号内部。默认是使用 "+"。
  • ! - 停止在输出字符串中使用货币符号。
  • - 如果使用 "-",所有字段左对齐。默认是右对齐。

字段宽度:

  • x- 规定字段的最小宽度(x)。默认是 0。
  • #x- 规定小数点左边数字的最大位数(x)。用于保持格式化输出在同一列对齐。如果数字位数大于 x,本规定将被忽略。
  • .x- 规定小数点右边数字的最大位数(x)。如果 x 是 0,小数点及其右边的数字将不会显示。默认使用本地设置。

转换字符:

  • i - 数字被格式化为国际货币格式。
  • n - 数字被格式化为国家货币格式。
  • % - 返回 % 字符。

注释:如果使用多个上述的格式值,它们必须按照上面的顺序进行使用,不能打乱。

注释:该函数受本地设置的影响。

number 必需。被插入到格式化字符串中 % 符号位置的数字。

technical details

return value: Returns the formatted string. Format strings preceding and following characters will remain the same return. Non-numeric digit returns NULL and generate E_WARNING.
PHP version: 4.3.0+


More examples

Example 1

International format with two small number (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 with () indicates a negative US international format right precision is 2, "*" is filled with characters:

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

The code above will output:

(******1234.57)



PHP String Reference PHP String Reference