(PHP 5 >= 5.3.0, PHP 7, PHP 8, 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.
These styles are used by the numfmt_create() to define the type of the formatter.
NumberFormatter::PATTERN_DECIMAL intNumberFormatter::DECIMAL intNumberFormatter::DECIMAL_COMPACT_SHORT intNumberFormatter::DECIMAL_COMPACT_LONG intNumberFormatter::CURRENCY intNumberFormatter::CURRENCY_ISO intNumberFormatter::CURRENCY_PLURAL intNumberFormatter::CASH_CURRENCY intNumberFormatter::CURRENCY_STANDARD intNumberFormatter::PERCENT intNumberFormatter::SCIENTIFIC intNumberFormatter::SPELLOUT intNumberFormatter::ORDINAL intNumberFormatter::DURATION intNumberFormatter::PATTERN_RULEBASED intNumberFormatter::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 intNumberFormatter::IGNORE intThese constants define how the numbers are parsed or formatted. They should be used as arguments to numfmt_format() and numfmt_parse().
NumberFormatter::TYPE_DEFAULT intNumberFormatter::TYPE_INT32 intNumberFormatter::TYPE_INT64 intNumberFormatter::TYPE_DOUBLE intNumberFormatter::TYPE_CURRENCY intNumber format attribute used by numfmt_get_attribute() and numfmt_set_attribute().
NumberFormatter::PARSE_INT_ONLY intNumberFormatter::GROUPING_USED intNumberFormatter::DECIMAL_ALWAYS_SHOWN intNumberFormatter::MAX_INTEGER_DIGITS intNumberFormatter::MIN_INTEGER_DIGITS intNumberFormatter::INTEGER_DIGITS intNumberFormatter::MAX_FRACTION_DIGITS intNumberFormatter::MIN_FRACTION_DIGITS intNumberFormatter::FRACTION_DIGITS intNumberFormatter::MULTIPLIER intNumberFormatter::GROUPING_SIZE intNumberFormatter::ROUNDING_MODE intNumberFormatter::ROUNDING_INCREMENT intNumberFormatter::FORMAT_WIDTH intNumberFormatter::PADDING_POSITION intNumberFormatter::SECONDARY_GROUPING_SIZE intNumberFormatter::SIGNIFICANT_DIGITS_USED intNumberFormatter::MIN_SIGNIFICANT_DIGITS intNumberFormatter::MAX_SIGNIFICANT_DIGITS intNumberFormatter::LENIENT_PARSE intNumber format text attribute used by numfmt_get_text_attribute() and numfmt_set_text_attribute().
NumberFormatter::POSITIVE_PREFIX intNumberFormatter::POSITIVE_SUFFIX intNumberFormatter::NEGATIVE_PREFIX intNumberFormatter::NEGATIVE_SUFFIX intNumberFormatter::PADDING_CHARACTER intNumberFormatter::CURRENCY_CODE intNumberFormatter::DEFAULT_RULESET intNumberFormatter::PUBLIC_RULESETS intNumber format symbols used by numfmt_get_symbol() and numfmt_set_symbol().
NumberFormatter::DECIMAL_SEPARATOR_SYMBOL intNumberFormatter::GROUPING_SEPARATOR_SYMBOL intNumberFormatter::PATTERN_SEPARATOR_SYMBOL intNumberFormatter::PERCENT_SYMBOL intNumberFormatter::ZERO_DIGIT_SYMBOL intNumberFormatter::DIGIT_SYMBOL intNumberFormatter::MINUS_SIGN_SYMBOL intNumberFormatter::PLUS_SIGN_SYMBOL intNumberFormatter::CURRENCY_SYMBOL intNumberFormatter::INTL_CURRENCY_SYMBOL intNumberFormatter::MONETARY_SEPARATOR_SYMBOL intNumberFormatter::EXPONENTIAL_SYMBOL intNumberFormatter::PERMILL_SYMBOL intNumberFormatter::PAD_ESCAPE_SYMBOL intNumberFormatter::INFINITY_SYMBOL intNumberFormatter::NAN_SYMBOL intNumberFormatter::SIGNIFICANT_DIGIT_SYMBOL intNumberFormatter::MONETARY_GROUPING_SEPARATOR_SYMBOL intRounding mode values used by numfmt_get_attribute() and numfmt_set_attribute() with NumberFormatter::ROUNDING_MODE attribute.
NumberFormatter::ROUND_AWAY_FROM_ZERONumberFormatter::ROUND_UP.NumberFormatter::ROUND_CEILING intNumberFormatter::ROUND_DOWN intNumberFormatter::ROUND_FLOOR intNumberFormatter::ROUND_HALFDOWN intNumberFormatter::ROUND_HALFEVEN intNumberFormatter::ROUND_HALFODDNumberFormatter::ROUND_HALFUP intNumberFormatter::ROUND_TOWARD_ZERONumberFormatter::ROUND_DOWN.NumberFormatter::ROUND_UP intPad position values used by numfmt_get_attribute() and numfmt_set_attribute() with NumberFormatter::PADDING_POSITION attribute.
NumberFormatter::PAD_AFTER_PREFIX intNumberFormatter::PAD_AFTER_SUFFIX intNumberFormatter::PAD_BEFORE_PREFIX intNumberFormatter::PAD_BEFORE_SUFFIX int| Version | Description |
|---|---|
| 8.5.0 | Added NumberFormatter::DECIMAL_COMPACT_SHORT, NumberFormatter::DECIMAL_COMPACT_LONG, NumberFormatter::CURRENCY_ISO, NumberFormatter::CURRENCY_PLURAL, NumberFormatter::CASH_CURRENCY, NumberFormatter::CURRENCY_STANDARD. |
| 8.4.0 | The class constants are now typed. |