(PHP 5 >= 5.3.0, PHP 7, PECL intl >= 1.0.0)
Programs store and operate on numbers using a locale-independent binary representation. When displaying or printing a number it is converted to a locale-specific string. For example, the number 12345.67 is "12,345.67" in the US, "12 345,67" in France and "12.345,67" in Germany.
By invoking the methods provided by the NumberFormatter class, you can format numbers, currencies, and percentages according to the specified or default locale. NumberFormatter is locale-sensitive so you need to create a new NumberFormatter for each locale. NumberFormatter methods format primitive-type numbers, such as double and output the number as a locale-specific string.
For currencies you can use currency format type to create a formatter that returns a string with the formatted number and the appropriate currency sign. Of course, the NumberFormatter class is unaware of exchange rates so, the number output is the same regardless of the specified currency. This means that the same number has different monetary values depending on the currency locale. If the number is 9988776.65 the results will be:
In order to format percentages, create a locale-specific formatter with percentage format type. With this formatter, a decimal fraction such as 0.75 is displayed as 75%.
For more complex formatting, like spelled-out numbers, the rule-based number formatters are used.
public static create ( string $locale , int $style [, string $pattern ] ) : NumberFormatter
public formatCurrency ( float $value , string $currency ) : string|false
public format ( int|float $value [, int $type ] ) : string
public getAttribute ( int $attr ) : int
public getErrorCode ( ) : int
public getErrorMessage ( ) : string
public getLocale ([ int $type ] ) : string
public getPattern ( ) : 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}
These styles are used by the numfmt_create() to define the type of the formatter.
NumberFormatter::PATTERN_DECIMAL
(int) NumberFormatter::DECIMAL
(int) NumberFormatter::CURRENCY
(int) NumberFormatter::PERCENT
(int) NumberFormatter::SCIENTIFIC
(int) NumberFormatter::SPELLOUT
(int) NumberFormatter::ORDINAL
(int) NumberFormatter::DURATION
(int) NumberFormatter::PATTERN_RULEBASED
(int) NumberFormatter::CURRENCY_ACCOUNTING
(int) ($3.00)
for negative currency amount instead of -$3.00
. Available as of PHP 7.4.1 and ICU 53. NumberFormatter::DEFAULT_STYLE
(int) NumberFormatter::IGNORE
(int) These constants define how the numbers are parsed or formatted. They should be used as arguments to numfmt_format() and numfmt_parse().
NumberFormatter::TYPE_DEFAULT
(int) NumberFormatter::TYPE_INT32
(int) NumberFormatter::TYPE_INT64
(int) NumberFormatter::TYPE_DOUBLE
(int) NumberFormatter::TYPE_CURRENCY
(int) Number format attribute used by numfmt_get_attribute() and numfmt_set_attribute().
NumberFormatter::PARSE_INT_ONLY
(int) NumberFormatter::GROUPING_USED
(int) NumberFormatter::DECIMAL_ALWAYS_SHOWN
(int) NumberFormatter::MAX_INTEGER_DIGITS
(int) NumberFormatter::MIN_INTEGER_DIGITS
(int) NumberFormatter::INTEGER_DIGITS
(int) NumberFormatter::MAX_FRACTION_DIGITS
(int) NumberFormatter::MIN_FRACTION_DIGITS
(int) NumberFormatter::FRACTION_DIGITS
(int) NumberFormatter::MULTIPLIER
(int) NumberFormatter::GROUPING_SIZE
(int) NumberFormatter::ROUNDING_MODE
(int) NumberFormatter::ROUNDING_INCREMENT
(int) NumberFormatter::FORMAT_WIDTH
(int) NumberFormatter::PADDING_POSITION
(int) NumberFormatter::SECONDARY_GROUPING_SIZE
(int) NumberFormatter::SIGNIFICANT_DIGITS_USED
(int) NumberFormatter::MIN_SIGNIFICANT_DIGITS
(int) NumberFormatter::MAX_SIGNIFICANT_DIGITS
(int) NumberFormatter::LENIENT_PARSE
(int) Number format text attribute used by numfmt_get_text_attribute() and numfmt_set_text_attribute().
NumberFormatter::POSITIVE_PREFIX
(int) NumberFormatter::POSITIVE_SUFFIX
(int) NumberFormatter::NEGATIVE_PREFIX
(int) NumberFormatter::NEGATIVE_SUFFIX
(int) NumberFormatter::PADDING_CHARACTER
(int) NumberFormatter::CURRENCY_CODE
(int) NumberFormatter::DEFAULT_RULESET
(int) NumberFormatter::PUBLIC_RULESETS
(int) Number format symbols used by numfmt_get_symbol() and numfmt_set_symbol().
NumberFormatter::DECIMAL_SEPARATOR_SYMBOL
(int) NumberFormatter::GROUPING_SEPARATOR_SYMBOL
(int) NumberFormatter::PATTERN_SEPARATOR_SYMBOL
(int) NumberFormatter::PERCENT_SYMBOL
(int) NumberFormatter::ZERO_DIGIT_SYMBOL
(int) NumberFormatter::DIGIT_SYMBOL
(int) NumberFormatter::MINUS_SIGN_SYMBOL
(int) NumberFormatter::PLUS_SIGN_SYMBOL
(int) NumberFormatter::CURRENCY_SYMBOL
(int) NumberFormatter::INTL_CURRENCY_SYMBOL
(int) NumberFormatter::MONETARY_SEPARATOR_SYMBOL
(int) NumberFormatter::EXPONENTIAL_SYMBOL
(int) NumberFormatter::PERMILL_SYMBOL
(int) NumberFormatter::PAD_ESCAPE_SYMBOL
(int) NumberFormatter::INFINITY_SYMBOL
(int) NumberFormatter::NAN_SYMBOL
(int) NumberFormatter::SIGNIFICANT_DIGIT_SYMBOL
(int) NumberFormatter::MONETARY_GROUPING_SEPARATOR_SYMBOL
(int) Rounding mode values used by numfmt_get_attribute() and numfmt_set_attribute() with NumberFormatter::ROUNDING_MODE
attribute.
NumberFormatter::ROUND_CEILING
(int) NumberFormatter::ROUND_DOWN
(int) NumberFormatter::ROUND_FLOOR
(int) NumberFormatter::ROUND_HALFDOWN
(int) NumberFormatter::ROUND_HALFEVEN
(int) NumberFormatter::ROUND_HALFUP
(int) NumberFormatter::ROUND_UP
(int) Pad position values used by numfmt_get_attribute() and numfmt_set_attribute() with NumberFormatter::PADDING_POSITION
attribute.
NumberFormatter::PAD_AFTER_PREFIX
(int) NumberFormatter::PAD_AFTER_SUFFIX
(int) NumberFormatter::PAD_BEFORE_PREFIX
(int) NumberFormatter::PAD_BEFORE_SUFFIX
(int)
© 1997–2020 The PHP Documentation Group
Licensed under the Creative Commons Attribution License v3.0 or later.
https://www.php.net/manual/en/class.numberformatter.php