NumberFormatter
class NumberFormatter (View source)
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.
Constants
| CURRENCY_ACCOUNTING |
|
| PATTERN_DECIMAL |
Decimal format defined by pattern |
| DECIMAL |
Decimal format |
| CURRENCY |
Currency format |
| PERCENT |
Percent format |
| SCIENTIFIC |
Scientific format |
| SPELLOUT |
Spellout rule-based format |
| ORDINAL |
Ordinal rule-based format |
| DURATION |
Duration rule-based format |
| PATTERN_RULEBASED |
Rule-based format defined by pattern |
| IGNORE |
Alias for PATTERN_DECIMAL |
| DEFAULT_STYLE |
Default format for the locale |
| ROUND_CEILING |
Rounding mode to round towards positive infinity. |
| ROUND_FLOOR |
Rounding mode to round towards negative infinity. |
| ROUND_DOWN |
Rounding mode to round towards zero. |
| ROUND_UP |
Rounding mode to round away from zero. |
| ROUND_HALFEVEN |
Rounding mode to round towards the "nearest neighbor" unless both
neighbors are equidistant, in which case, round towards the even
neighbor. |
| ROUND_HALFDOWN |
Rounding mode to round towards "nearest neighbor" unless both neighbors
are equidistant, in which case round down. |
| ROUND_HALFUP |
Rounding mode to round towards "nearest neighbor" unless both neighbors
are equidistant, in which case round up. |
| PAD_BEFORE_PREFIX |
Pad characters inserted before the prefix. |
| PAD_AFTER_PREFIX |
Pad characters inserted after the prefix. |
| PAD_BEFORE_SUFFIX |
Pad characters inserted before the suffix. |
| PAD_AFTER_SUFFIX |
Pad characters inserted after the suffix. |
| PARSE_INT_ONLY |
Parse integers only. |
| GROUPING_USED |
Use grouping separator. |
| DECIMAL_ALWAYS_SHOWN |
Always show decimal point. |
| MAX_INTEGER_DIGITS |
Maximum integer digits. |
| MIN_INTEGER_DIGITS |
Minimum integer digits. |
| INTEGER_DIGITS |
Integer digits. |
| MAX_FRACTION_DIGITS |
Maximum fraction digits. |
| MIN_FRACTION_DIGITS |
Minimum fraction digits. |
| FRACTION_DIGITS |
Fraction digits. |
| MULTIPLIER |
Multiplier. |
| GROUPING_SIZE |
Grouping size. |
| ROUNDING_MODE |
Rounding Mode. |
| ROUNDING_INCREMENT |
Rounding increment. |
| FORMAT_WIDTH |
The width to which the output of format() is padded. |
| PADDING_POSITION |
The position at which padding will take place. See pad position
constants for possible argument values. |
| SECONDARY_GROUPING_SIZE |
Secondary grouping size. |
| SIGNIFICANT_DIGITS_USED |
Use significant digits. |
| MIN_SIGNIFICANT_DIGITS |
Minimum significant digits. |
| MAX_SIGNIFICANT_DIGITS |
Maximum significant digits. |
| LENIENT_PARSE |
Lenient parse mode used by rule-based formats. |
| POSITIVE_PREFIX |
Positive prefix. |
| POSITIVE_SUFFIX |
Positive suffix. |
| NEGATIVE_PREFIX |
Negative prefix. |
| NEGATIVE_SUFFIX |
Negative suffix. |
| PADDING_CHARACTER |
The character used to pad to the format width. |
| CURRENCY_CODE |
The ISO currency code. |
| DEFAULT_RULESET |
The default rule set. This is only available with rule-based
formatters. |
| 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). |
| DECIMAL_SEPARATOR_SYMBOL |
The decimal separator. |
| GROUPING_SEPARATOR_SYMBOL |
The grouping separator. |
| PATTERN_SEPARATOR_SYMBOL |
The pattern separator. |
| PERCENT_SYMBOL |
The percent sign. |
| ZERO_DIGIT_SYMBOL |
Zero. |
| DIGIT_SYMBOL |
Character representing a digit in the pattern. |
| MINUS_SIGN_SYMBOL |
The minus sign. |
| PLUS_SIGN_SYMBOL |
The plus sign. |
| CURRENCY_SYMBOL |
The currency symbol. |
| INTL_CURRENCY_SYMBOL |
The international currency symbol. |
| MONETARY_SEPARATOR_SYMBOL |
The monetary separator. |
| EXPONENTIAL_SYMBOL |
The exponential symbol. |
| PERMILL_SYMBOL |
Per mill symbol. |
| PAD_ESCAPE_SYMBOL |
Escape padding character. |
| INFINITY_SYMBOL |
Infinity symbol. |
| NAN_SYMBOL |
Not-a-number symbol. |
| SIGNIFICANT_DIGIT_SYMBOL |
Significant digit symbol. |
| MONETARY_GROUPING_SEPARATOR_SYMBOL |
The monetary grouping separator. |
| TYPE_DEFAULT |
Derive the type from variable type |
| TYPE_INT32 |
Format/parse as 32-bit integer |
| TYPE_INT64 |
Format/parse as 64-bit integer |
| TYPE_DOUBLE |
Format/parse as floating point value |
| deprecated TYPE_CURRENCY |
Format/parse as currency value |
|
ROUND_TOWARD_ZERO
Since: 8.4
|
|
|
ROUND_AWAY_FROM_ZERO
Since: 8.4
|
|
|
ROUND_HALFODD
Since: 8.4
|
|
|
DECIMAL_COMPACT_SHORT
Since: 8.5
|
|
|
DECIMAL_COMPACT_LONG
Since: 8.5
|
|
|
CURRENCY_ISO
Since: 8.5
|
|
|
CURRENCY_PLURAL
Since: 8.5
|
|
|
CASH_CURRENCY
Since: 8.5
|
|
|
CURRENCY_STANDARD
Since: 8.5
|
|
Methods
Create a number formatter
(PHP 5 >= 5.3.0, PECL intl >= 1.0.0)
Create a number formatter
(PHP 5 >= 5.3.0, PECL intl >= 1.0.0)
Format a number
(PHP 5 >= 5.3.0, PECL intl >= 1.0.0)
Parse a number
(PHP 5 >= 5.3.0, PECL intl >= 1.0.0)
Format a currency value
(PHP 5 >= 5.3.0, PECL intl >= 1.0.0)
Parse a currency number
(PHP 5 >= 5.3.0, PECL intl >= 1.0.0)
Set an attribute
(PHP 5 >= 5.3.0, PECL intl >= 1.0.0)
Get an attribute
(PHP 5 >= 5.3.0, PECL intl >= 1.0.0)
Set a text attribute
(PHP 5 >= 5.3.0, PECL intl >= 1.0.0)
Get a text attribute
(PHP 5 >= 5.3.0, PECL intl >= 1.0.0)
Set a symbol value
(PHP 5 >= 5.3.0, PECL intl >= 1.0.0)
Get a symbol value
(PHP 5 >= 5.3.0, PECL intl >= 1.0.0)
Set formatter pattern
(PHP 5 >= 5.3.0, PECL intl >= 1.0.0)
Get formatter pattern
(PHP 5 >= 5.3.0, PECL intl >= 1.0.0)
Get formatter locale
(PHP 5 >= 5.3.0, PECL intl >= 1.0.0)
Get formatter's last error code.
(PHP 5 >= 5.3.0, PECL intl >= 1.0.0)
Get formatter's last error message.
Details
__construct(string $locale, int $style, string $pattern = null)
Create a number formatter
Creates a number formatter.
static NumberFormatter|null
create(string $locale, int $style, string $pattern = null)
(PHP 5 >= 5.3.0, PECL intl >= 1.0.0)
Create a number formatter
string|false
format(int|float $num, int $type = 0)
(PHP 5 >= 5.3.0, PECL intl >= 1.0.0)
Format a number
int|float|false
parse(string $string, int $type = NumberFormatter::TYPE_DOUBLE, int $offset = null)
(PHP 5 >= 5.3.0, PECL intl >= 1.0.0)
Parse a number
string|false
formatCurrency(float $amount, string $currency)
(PHP 5 >= 5.3.0, PECL intl >= 1.0.0)
Format a currency value
float|false
parseCurrency(string $string, string $currency, int $offset = null)
(PHP 5 >= 5.3.0, PECL intl >= 1.0.0)
Parse a currency number
bool
setAttribute(int $attribute, int $value)
(PHP 5 >= 5.3.0, PECL intl >= 1.0.0)
Set an attribute
int|float|false
getAttribute(int $attribute)
(PHP 5 >= 5.3.0, PECL intl >= 1.0.0)
Get an attribute
bool
setTextAttribute(int $attribute, string $value)
(PHP 5 >= 5.3.0, PECL intl >= 1.0.0)
Set a text attribute
string|false
getTextAttribute(int $attribute)
(PHP 5 >= 5.3.0, PECL intl >= 1.0.0)
Get a text attribute
bool
setSymbol(int $symbol, string $value)
(PHP 5 >= 5.3.0, PECL intl >= 1.0.0)
Set a symbol value
string|false
getSymbol(int $symbol)
(PHP 5 >= 5.3.0, PECL intl >= 1.0.0)
Get a symbol value
bool
setPattern(string $pattern)
(PHP 5 >= 5.3.0, PECL intl >= 1.0.0)
Set formatter pattern
string|false
getPattern()
(PHP 5 >= 5.3.0, PECL intl >= 1.0.0)
Get formatter pattern
string|false
getLocale(int $type = 0)
(PHP 5 >= 5.3.0, PECL intl >= 1.0.0)
Get formatter locale
int
getErrorCode()
(PHP 5 >= 5.3.0, PECL intl >= 1.0.0)
Get formatter's last error code.
string
getErrorMessage()
(PHP 5 >= 5.3.0, PECL intl >= 1.0.0)
Get formatter's last error message.