The NumberFormatter class

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

Introduction

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:

  • 9 988 776,65 € in France
  • 9.988.776,65 € in Germany
  • $9,988,776.65 in the United States

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.

Class synopsis

classNumberFormatter {
publicconstintPATTERN_DECIMAL;
publicconstintDECIMAL;
publicconstintCURRENCY;
publicconstintPERCENT;
publicconstintSCIENTIFIC;
publicconstintSPELLOUT;
publicconstintORDINAL;
publicconstintDURATION;
publicconstintIGNORE;
publicconstintDEFAULT_STYLE;
publicconstintROUND_CEILING;
publicconstintROUND_FLOOR;
publicconstintROUND_DOWN;
publicconstintROUND_UP;
publicconstintROUND_HALFEVEN;
publicconstintROUND_HALFDOWN;
publicconstintROUND_HALFUP;
publicconstintPARSE_INT_ONLY;
publicconstintGROUPING_USED;
publicconstintINTEGER_DIGITS;
publicconstintFRACTION_DIGITS;
publicconstintMULTIPLIER;
publicconstintGROUPING_SIZE;
publicconstintROUNDING_MODE;
publicconstintFORMAT_WIDTH;
publicconstintLENIENT_PARSE;
publicconstintPOSITIVE_PREFIX;
publicconstintPOSITIVE_SUFFIX;
publicconstintNEGATIVE_PREFIX;
publicconstintNEGATIVE_SUFFIX;
publicconstintCURRENCY_CODE;
publicconstintDEFAULT_RULESET;
publicconstintPUBLIC_RULESETS;
publicconstintPERCENT_SYMBOL;
publicconstintDIGIT_SYMBOL;
publicconstintCURRENCY_SYMBOL;
publicconstintPERMILL_SYMBOL;
publicconstintINFINITY_SYMBOL;
publicconstintNAN_SYMBOL;
publicconstintTYPE_DEFAULT;
publicconstintTYPE_INT32;
publicconstintTYPE_INT64;
publicconstintTYPE_DOUBLE;
publicconstintTYPE_CURRENCY;
public__construct(string$locale, int$style, ?string$pattern = null)
publicstaticcreate(string$locale, int$style, ?string$pattern = null): ?NumberFormatter
publicformatCurrency(float$amount, string$currency): string|false
publicformat(int|float$num, int$type = NumberFormatter::TYPE_DEFAULT): string|false
publicgetAttribute(int$attribute): int|float|false
publicgetErrorCode(): int
publicgetLocale(int$type = ULOC_ACTUAL_LOCALE): string|false
publicgetSymbol(int$symbol): string|false
publicgetTextAttribute(int$attribute): string|false
publicparseCurrency(string$string, string&$currency, int&$offset = null): float|false
publicparse(string$string, int$type = NumberFormatter::TYPE_DOUBLE, int&$offset = null): int|float|false
publicsetAttribute(int$attribute, int|float$value): bool
publicsetPattern(string$pattern): bool
publicsetSymbol(int$symbol, string$value): bool
publicsetTextAttribute(int$attribute, string$value): bool
}

Predefined Constants

These styles are used by the numfmt_create() to define the type of the formatter.

NumberFormatter::PATTERN_DECIMAL
Decimal format defined by pattern
NumberFormatter::DECIMAL
Decimal format
NumberFormatter::CURRENCY
Currency format
NumberFormatter::PERCENT
Percent format
NumberFormatter::SCIENTIFIC
Scientific format
NumberFormatter::SPELLOUT
Spellout rule-based format
NumberFormatter::ORDINAL
Ordinal rule-based format
NumberFormatter::DURATION
Duration rule-based format
NumberFormatter::PATTERN_RULEBASED
Rule-based format defined by pattern
NumberFormatter::CURRENCY_ACCOUNTING
Currency format for accounting, e.g., ($3.00) for negative currency amount instead of -$3.00. Available as of PHP 7.4.1 and ICU 53.
NumberFormatter::DEFAULT_STYLE
Default format for the locale
NumberFormatter::IGNORE
Alias for PATTERN_DECIMAL

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
Derive the type from variable type
NumberFormatter::TYPE_INT32
Format/parse as 32-bit integer
NumberFormatter::TYPE_INT64
Format/parse as 64-bit integer
NumberFormatter::TYPE_DOUBLE
Format/parse as floating point value
NumberFormatter::TYPE_CURRENCY
Format/parse as currency value. Deprecated as of PHP 8.3.0

Number format attribute used by numfmt_get_attribute() and numfmt_set_attribute().

NumberFormatter::PARSE_INT_ONLY
Parse integers only.
NumberFormatter::GROUPING_USED
Use grouping separator.
NumberFormatter::DECIMAL_ALWAYS_SHOWN
Always show decimal point.
NumberFormatter::MAX_INTEGER_DIGITS
Maximum integer digits.
NumberFormatter::MIN_INTEGER_DIGITS
Minimum integer digits.
NumberFormatter::INTEGER_DIGITS
Integer digits.
NumberFormatter::MAX_FRACTION_DIGITS
Maximum fraction digits.
NumberFormatter::MIN_FRACTION_DIGITS
Minimum fraction digits.
NumberFormatter::FRACTION_DIGITS
Fraction digits.
NumberFormatter::MULTIPLIER
Multiplier.
NumberFormatter::GROUPING_SIZE
Grouping size.
NumberFormatter::ROUNDING_MODE
Rounding Mode.
NumberFormatter::ROUNDING_INCREMENT
Rounding increment.
NumberFormatter::FORMAT_WIDTH
The width to which the output of format() is padded.
NumberFormatter::PADDING_POSITION
The position at which padding will take place. See pad position constants for possible argument values.
NumberFormatter::SECONDARY_GROUPING_SIZE
Secondary grouping size.
NumberFormatter::SIGNIFICANT_DIGITS_USED
Use significant digits.
NumberFormatter::MIN_SIGNIFICANT_DIGITS
Minimum significant digits.
NumberFormatter::MAX_SIGNIFICANT_DIGITS
Maximum significant digits.
NumberFormatter::LENIENT_PARSE
Lenient parse mode used by rule-based formats.

Number format text attribute used by numfmt_get_text_attribute() and numfmt_set_text_attribute().

NumberFormatter::POSITIVE_PREFIX
Positive prefix.
NumberFormatter::POSITIVE_SUFFIX
Positive suffix.
NumberFormatter::NEGATIVE_PREFIX
Negative prefix.
NumberFormatter::NEGATIVE_SUFFIX
Negative suffix.
NumberFormatter::PADDING_CHARACTER
The character used to pad to the format width.
NumberFormatter::CURRENCY_CODE
The ISO currency code.
NumberFormatter::DEFAULT_RULESET
The default rule set. This is only available with rule-based formatters.
NumberFormatter::PUBLIC_RULESETS
The public rule sets. This is only available with rule-based formatters. This is a read-only attribute. The public rulesets are returned as a single string, with each ruleset name delimited by ';' (semicolon).

Rounding mode values used by numfmt_get_attribute() and numfmt_set_attribute() with NumberFormatter::ROUNDING_MODE attribute.

NumberFormatter::ROUND_CEILING
Rounding mode to round towards positive infinity.
NumberFormatter::ROUND_DOWN
Rounding mode to round towards zero.
NumberFormatter::ROUND_FLOOR
Rounding mode to round towards negative infinity.
NumberFormatter::ROUND_HALFDOWN
Rounding mode to round towards "nearest neighbor" unless both neighbors are equidistant, in which case round down.
NumberFormatter::ROUND_HALFEVEN
Rounding mode to round towards the "nearest neighbor" unless both neighbors are equidistant, in which case, round towards the even neighbor.
NumberFormatter::ROUND_HALFUP
Rounding mode to round towards "nearest neighbor" unless both neighbors are equidistant, in which case round up.
NumberFormatter::ROUND_UP
Rounding mode to round away from zero.

Pad position values used by numfmt_get_attribute() and numfmt_set_attribute() with NumberFormatter::PADDING_POSITION attribute.

NumberFormatter::PAD_AFTER_PREFIX
Pad characters inserted after the prefix.
NumberFormatter::PAD_AFTER_SUFFIX
Pad characters inserted after the suffix.
NumberFormatter::PAD_BEFORE_PREFIX
Pad characters inserted before the prefix.
NumberFormatter::PAD_BEFORE_SUFFIX
Pad characters inserted before the suffix.

Table of Contents

To Top