当前位置: 首页 > 网络学院 > 服务端脚本教程 > PHP > money_format()函数

PHP
PHP 介绍
PHP 安装
PHP 语法
PHP 变量
PHP操作符
PHP If...Else
PHP Switch
PHP 数组
PHP 循环
PHP 函数
PHP 表单
PHP $_GET
PHP $_POST
PHP Date
PHP Include
PHP 文件处理
PHP 文件上传
PHP Cookies
PHP Sessions
PHP 发送邮件

PHP 中的 money_format()函数


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-03-04   浏览: 619 ::
收藏到网摘: n/a

Definition and Usage
定义和用法

The money_format() function returns a string formatted as a currency string.
money_format()函数的作用是:将数字格式化为货币形式。

This function inserts a formatted number where there is a percent (%) sign in the main string.
通过这个函数,我们将插入一个格式化的数字。在这个函数的定义过程中,我们将在string参数前加上“%”。

Syntax
语法

money_format(string,number)

Parameter参数 Description描述
string Required. Specifies the string to be formatted and how to format the variables in it.
必要参数。指定需要被格式化的字符串以及以何种方式去定义它的格式

Possible format values:
可使用的格式值:

Padding and Flags:
补白和标记:

  • =f - Specifies the character (f) to be used as padding (Example: %=t this uses "t" as padding). Default is space
    =f – 指定用于补白的字符串(案例:%=t :使用“t”作为补白)。默认是空格
  • ^ - Removes the use of grouping characters
    ^ - 删除对分组字符的使用
  • + or ( - Specifies how to show positive and negative numbers. If "+" is used, the local setting for + and - will be used (usually a sign in front of negative numbers, and nothing in front of positive numbers). If "(" is used, negative numbers are enclosed in parenthesis. Default is "+"
    + 或 ( - 指定如果显示正数和负数。如果设置为“+”,那么在本地设置中将使用“+”和“-”(通常只在负数前显示“-”,正数之前不显示符号)。如果是" (如使用",负数将被显示在圆括号内) 默认是“+”)
  • ! - Stops the use of currency symbols in the output string
    ! – 禁止在输出的字符串内使用货币符号
  • - If "-" is used, all fields are left-justified. Default is right-justified
    - 如果使用“-”,所有的字段将左对齐。默认为右对齐

Field width:
字段宽度:

  • x - Specifies the minimum field width (x). Default is 0
    x – 指定字段的最大宽度(x)。默认值为0
  • #x - Specifies the maximum number (x) of digits expected to the left of the decimal point. This is used to keep formatted output aligned in the same columns. If the number of digits are bigger than x, this specification is ignored
    #x – 指定位于小数点左边的数字的最大宽度。它将以同列对齐的格式输出。如果给定的数字位数大于x,那么这个超出宽度的部分将被忽略
  • .x - Specifies the maximum number (x) of digits expected to the right of the decimal point. If x is 0, the decimal point and the digits to it's right will not be shown. Default is local settings
    .x -指定位于小数点右边的数字的最大宽度。如果x的值为0,那么小数部分将不会显示。默认值为本地设置的值

Conversion characters:
字符转换:

  • i - The number is formatted to international currency format
    i – 以国际通用的货币形式定义格式
  • n - The number is formatted to national currency format
    n -以本国通用的货币形式定义格式
  • % - Returns the % character
    % - 返回%字符

Note: If multiple format values are used, they must be in the same order as shown above.
注意:如果使用多个格式值,它们必须具有相同的格式

Note: This function is affected by local settings.
注意:这个函数采用的是本地设置

number Required. The number to be inserted at the %-sign in the format string
必要参数。在格式化的字符串前插入%符号


Tips and Notes
注意点

Note: The money_format() function does not work on Windows platforms.
注意:Windows操作平台不支持money_format()函数。


Example 1
案例1

International en_US format:
国际通用的en_US格式:

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

The output of the code above will be:
上述代码将输出下面的结果:

The price is USD 1,234.56


Example 2
案例2

National Norwegian format with 2 decimals:
挪威格式,2位小数:

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

The output of the code above will be:
上述代码将输出下面的结果:

NOK 1.234,56


Example 3
案例3

Negative number, US national format with () to indicate negative numbers and 2 digits of right precision and "*" as fill character:
负数情况,美国的格式使用()来表示复数,设值为两位小数,并在数字的左边以“*”作为填充字符串:

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

The output of the code above will be:
上述代码将输出下面的结果:

($********1,234.57)

评论 (0) All

登陆 | 还没注册?