NumberFormatter

class NumberFormatter

Replacement for PHP's native {@link \NumberFormatter} class.

The only methods currently supported in this class are:

  • {@link __construct}
  • {@link create}
  • {@link formatCurrency}
  • {@link format}
  • {@link getAttribute}
  • {@link getErrorCode}
  • {@link getErrorMessage}
  • {@link getLocale}
  • {@link parse}
  • {@link setAttribute}

Constants

PATTERN_DECIMAL
DECIMAL
CURRENCY
PERCENT
SCIENTIFIC
SPELLOUT
ORDINAL
DURATION
PATTERN_RULEBASED
IGNORE
DEFAULT_STYLE
TYPE_DEFAULT
TYPE_INT32
TYPE_INT64
TYPE_DOUBLE
TYPE_CURRENCY
PARSE_INT_ONLY
GROUPING_USED
DECIMAL_ALWAYS_SHOWN
MAX_INTEGER_DIGITS
MIN_INTEGER_DIGITS
INTEGER_DIGITS
MAX_FRACTION_DIGITS
MIN_FRACTION_DIGITS
FRACTION_DIGITS
MULTIPLIER
GROUPING_SIZE
ROUNDING_MODE
ROUNDING_INCREMENT
FORMAT_WIDTH
PADDING_POSITION
SECONDARY_GROUPING_SIZE
SIGNIFICANT_DIGITS_USED
MIN_SIGNIFICANT_DIGITS
MAX_SIGNIFICANT_DIGITS
LENIENT_PARSE
POSITIVE_PREFIX
POSITIVE_SUFFIX
NEGATIVE_PREFIX
NEGATIVE_SUFFIX
PADDING_CHARACTER
CURRENCY_CODE
DEFAULT_RULESET
PUBLIC_RULESETS
DECIMAL_SEPARATOR_SYMBOL
GROUPING_SEPARATOR_SYMBOL
PATTERN_SEPARATOR_SYMBOL
PERCENT_SYMBOL
ZERO_DIGIT_SYMBOL
DIGIT_SYMBOL
MINUS_SIGN_SYMBOL
PLUS_SIGN_SYMBOL
CURRENCY_SYMBOL
INTL_CURRENCY_SYMBOL
MONETARY_SEPARATOR_SYMBOL
EXPONENTIAL_SYMBOL
PERMILL_SYMBOL
PAD_ESCAPE_SYMBOL
INFINITY_SYMBOL
NAN_SYMBOL
SIGNIFICANT_DIGIT_SYMBOL
MONETARY_GROUPING_SEPARATOR_SYMBOL
ROUND_CEILING
ROUND_FLOOR
ROUND_DOWN
ROUND_UP
ROUND_HALFEVEN
ROUND_HALFDOWN
ROUND_HALFUP
PAD_BEFORE_PREFIX
PAD_AFTER_PREFIX
PAD_BEFORE_SUFFIX
PAD_AFTER_SUFFIX

Methods

__construct(string $locale = 'en', int $style = null, string $pattern = null)
static NumberFormatter create(string $locale = 'en', int $style = null, string $pattern = null)

Static constructor.

string formatCurrency(float $value, string $currency)

Format a currency value.

bool|string format(int|float $value, int $type = self::TYPE_DEFAULT)

Format a number.

bool|int getAttribute(int $attr)

Returns an attribute value.

int getErrorCode()

Returns formatter's last error code. Always returns the UZEROERROR class constant value.

string getErrorMessage()

Returns formatter's last error message. Always returns the UZEROERROR_MESSAGE class constant value.

string getLocale(int $type = Locale::ACTUAL_LOCALE)

Returns the formatter's locale.

bool|string getPattern()

Not supported. Returns the formatter's pattern.

bool|string getSymbol(int $attr)

Not supported. Returns a formatter symbol value.

bool|string getTextAttribute(int $attr)

Not supported. Returns a formatter text attribute value.

bool|string parseCurrency(string $value, string $currency, int $position = null)

Not supported. Parse a currency number.

int|float|false parse(string $value, int $type = self::TYPE_DOUBLE, int $position)

Parse a number.

bool setAttribute(int $attr, int $value)

Set an attribute.

bool setPattern(string $pattern)

Not supported. Set the formatter's pattern.

bool setSymbol(int $attr, string $value)

Not supported. Set the formatter's symbol.

bool setTextAttribute(int $attr, int $value)

Not supported. Set a text attribute.

Details

__construct(string $locale = 'en', int $style = null, string $pattern = null)

Parameters

string $locale The locale code. The only currently supported locale is "en" (or null using the default locale, i.e. "en")
int $style Style of the formatting, one of the format style constants. The only supported styles are NumberFormatter::DECIMAL and NumberFormatter::CURRENCY.
string $pattern Not supported. A pattern string in case $style is NumberFormat::PATTERNDECIMAL or NumberFormat::PATTERNRULEBASED. It must conform to the syntax described in the ICU DecimalFormat or ICU RuleBasedNumberFormat documentation

Exceptions

MethodArgumentValueNotImplementedException When $locale different than "en" or null is passed
MethodArgumentValueNotImplementedException When the $style is not supported
MethodArgumentNotImplementedException When the pattern value is different than null

See also

http://www.php.net/manual/en/numberformatter.create.php
http://www.icu-project.org/apiref/icu4c/classDecimalFormat.html#_details
http://www.icu-project.org/apiref/icu4c/classRuleBasedNumberFormat.html#_details

static NumberFormatter create(string $locale = 'en', int $style = null, string $pattern = null)

Static constructor.

Parameters

string $locale The locale code. The only supported locale is "en" (or null using the default locale, i.e. "en")
int $style Style of the formatting, one of the format style constants. The only currently supported styles are NumberFormatter::DECIMAL and NumberFormatter::CURRENCY.
string $pattern Not supported. A pattern string in case $style is NumberFormat::PATTERNDECIMAL or NumberFormat::PATTERNRULEBASED. It must conform to the syntax described in the ICU DecimalFormat or ICU RuleBasedNumberFormat documentation

Return Value

NumberFormatter

Exceptions

MethodArgumentValueNotImplementedException When $locale different than "en" or null is passed
MethodArgumentValueNotImplementedException When the $style is not supported
MethodArgumentNotImplementedException When the pattern value is different than null

See also

http://www.php.net/manual/en/numberformatter.create.php
http://www.icu-project.org/apiref/icu4c/classDecimalFormat.html#_details
http://www.icu-project.org/apiref/icu4c/classRuleBasedNumberFormat.html#_details

string formatCurrency(float $value, string $currency)

Format a currency value.

Parameters

float $value The numeric currency value
string $currency The 3-letter ISO 4217 currency code indicating the currency to use

Return Value

string The formatted currency value

See also

http://www.php.net/manual/en/numberformatter.formatcurrency.php
https://en.wikipedia.org/wiki/ISO_4217#Active_codes

bool|string format(int|float $value, int $type = self::TYPE_DEFAULT)

Format a number.

Parameters

int|float $value the value to format
int $type Type of the formatting, one of the format type constants. Only type NumberFormatter::TYPE_DEFAULT is currently supported.

Return Value

bool|string The formatted value or false on error

Exceptions

NotImplementedException If the method is called with the class $style 'CURRENCY'
MethodArgumentValueNotImplementedException If the $type is different than TYPE_DEFAULT

See also

http://www.php.net/manual/en/numberformatter.format.php

bool|int getAttribute(int $attr)

Returns an attribute value.

Parameters

int $attr An attribute specifier, one of the numeric attribute constants

Return Value

bool|int The attribute value on success or false on error

See also

http://www.php.net/manual/en/numberformatter.getattribute.php

int getErrorCode()

Returns formatter's last error code. Always returns the UZEROERROR class constant value.

Return Value

int The error code from last formatter call

See also

http://www.php.net/manual/en/numberformatter.geterrorcode.php

string getErrorMessage()

Returns formatter's last error message. Always returns the UZEROERROR_MESSAGE class constant value.

Return Value

string The error message from last formatter call

See also

http://www.php.net/manual/en/numberformatter.geterrormessage.php

string getLocale(int $type = Locale::ACTUAL_LOCALE)

Returns the formatter's locale.

The parameter $type is currently ignored.

Parameters

int $type Not supported. The locale name type to return (Locale::VALIDLOCALE or Locale::ACTUALLOCALE)

Return Value

string The locale used to create the formatter. Currently always returns "en".

See also

http://www.php.net/manual/en/numberformatter.getlocale.php

bool|string getPattern()

Not supported. Returns the formatter's pattern.

Return Value

bool|string The pattern string used by the formatter or false on error

Exceptions

MethodNotImplementedException

See also

http://www.php.net/manual/en/numberformatter.getpattern.php

bool|string getSymbol(int $attr)

Not supported. Returns a formatter symbol value.

Parameters

int $attr A symbol specifier, one of the format symbol constants

Return Value

bool|string The symbol value or false on error

See also

http://www.php.net/manual/en/numberformatter.getsymbol.php

bool|string getTextAttribute(int $attr)

Not supported. Returns a formatter text attribute value.

Parameters

int $attr An attribute specifier, one of the text attribute constants

Return Value

bool|string The attribute value or false on error

See also

http://www.php.net/manual/en/numberformatter.gettextattribute.php

bool|string parseCurrency(string $value, string $currency, int $position = null)

Not supported. Parse a currency number.

Parameters

string $value The value to parse
string $currency Parameter to receive the currency name (reference)
int $position Offset to begin the parsing on return this value will hold the offset at which the parsing ended

Return Value

bool|string The parsed numeric value of false on error

Exceptions

MethodNotImplementedException

See also

http://www.php.net/manual/en/numberformatter.parsecurrency.php

int|float|false parse(string $value, int $type = self::TYPE_DOUBLE, int $position)

Parse a number.

Parameters

string $value the value to parse
int $type Type of the formatting, one of the format type constants. NumberFormatter::TYPE_DOUBLE by default.
int $position offset to begin the parsing on return this value will hold the offset at which the parsing ended

Return Value

int|float|false The parsed value of false on error

See also

http://www.php.net/manual/en/numberformatter.parse.php

bool setAttribute(int $attr, int $value)

Set an attribute.

Parameters

int $attr An attribute specifier, one of the numeric attribute constants. The only currently supported attributes are NumberFormatter::FRACTIONDIGITS, NumberFormatter::GROUPINGUSED and NumberFormatter::ROUNDING_MODE.
int $value the attribute value

Return Value

bool true on success or false on failure

Exceptions

MethodArgumentValueNotImplementedException When the $attr is not supported
MethodArgumentValueNotImplementedException When the $value is not supported

See also

http://www.php.net/manual/en/numberformatter.setattribute.php

bool setPattern(string $pattern)

Not supported. Set the formatter's pattern.

Parameters

string $pattern A pattern string in conformance with the ICU DecimalFormat documentation

Return Value

bool true on success or false on failure

Exceptions

MethodNotImplementedException

See also

http://www.php.net/manual/en/numberformatter.setpattern.php
http://www.icu-project.org/apiref/icu4c/classDecimalFormat.html#_details

bool setSymbol(int $attr, string $value)

Not supported. Set the formatter's symbol.

Parameters

int $attr A symbol specifier, one of the format symbol constants
string $value The value for the symbol

Return Value

bool true on success or false on failure

Exceptions

MethodNotImplementedException

See also

http://www.php.net/manual/en/numberformatter.setsymbol.php

bool setTextAttribute(int $attr, int $value)

Not supported. Set a text attribute.

Parameters

int $attr An attribute specifier, one of the text attribute constants
int $value The attribute value

Return Value

bool true on success or false on failure

Exceptions

MethodNotImplementedException

See also

http://www.php.net/manual/en/numberformatter.settextattribute.php

© 2004–2017 Fabien Potencier
Licensed under the MIT License.
http://api.symfony.com/2.7/Symfony/Component/Intl/NumberFormatter/NumberFormatter.html