• 首页
  • vue
  • TypeScript
  • JavaScript
  • scss
  • css3
  • html5
  • php
  • MySQL
  • redis
  • jQuery
  • 位置: php 中文手册 -> 国际化功能

    NumberFormatter类

    (PHP 5 >= 5.3.0, PHP 7, PECL intl >= 1.0.0)

    程序使用与语言环境无关的二进制表示来存储和操作数字。显示或打印数字时,它将转换为特定于区域设置的字符串。例如,12345.67在美国是“12345.67”,在法国是“12345,67”,在德国是“12.345,67”。

    通过调用NumberFormatter类提供的方法,可以根据指定的或默认的区域设置格式化数字、货币和百分比。NumberFormatter对区域设置敏感,因此需要为每个区域设置创建一个新的NumberFormatter。NumberFormatter方法格式化基本类型数字,例如double,并将数字输出为特定于区域设置的字符串。

    对于货币,可以使用货币格式类型创建一个格式设置程序,该格式设置程序返回一个带格式数字和适当货币符号的字符串。当然,NumberFormatter类不知道汇率,因此,无论指定的货币是什么,数字输出都是相同的。这意味着同一个数字根据货币区域设置具有不同的货币值。如果数字为9988776.65,则结果为:

    • 法国:988776
    • 德国为:9.988.776,65
    • 美国:9988776.65美元

    为了格式化百分比,请使用百分比格式类型创建特定于区域设置的格式化程序。使用此格式化程序,小数部分(如0.75)显示为75%。对于更复杂的格式,如拼写数字,使用基于规则的数字格式设置程序。

    NumberFormatter
    {
    	/* 方法 */
    	public __construct ( string $locale , int $style [, string $pattern ] )
    	public static create ( string $locale , int $style [, string $pattern ] ) : NumberFormatter
    	public formatCurrency ( float $value , string $currency ) : string
    	public format ( number $value [, int $type ] ) : string
    	public getAttribute ( int $attr ) : int
    	public getErrorCode ( void ) : int
    	public getErrorMessage ( void ) : string
    	public getLocale ([ int $type ] ) : string
    	public getPattern ( void ) : string
    	public getSymbol ( int $attr ) : string
    	public getTextAttribute ( int $attr ) : string
    	public parseCurrency ( string $value , string &$currency [, int &$position ] ) : float
    	public parse ( string $value [, int $type [, int &$position ]] ) : mixed
    	public setAttribute ( int $attr , int $value ) : bool
    	public setPattern ( string $pattern ) : bool
    	public setSymbol ( int $attr , string $value ) : bool
    	public setTextAttribute ( int $attr , string $value ) : bool
    }