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

Properties

protected int $errorCode The error code from the last operation.
protected string $errorMessage The error message from the last operation.

Methods

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

No description

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 U_ZERO_ERROR class constant value.

string
getErrorMessage()

Returns formatter's last error message. Always returns the U_ZERO_ERROR_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 = 0)

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, string $value)

Not supported. Set a text attribute.

resetError()

Set the error to the default U_ZERO_ERROR.

Details

at line 260
__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::PATTERN_DECIMAL or NumberFormat::PATTERN_RULEBASED. 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

at line 299
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::PATTERN_DECIMAL or NumberFormat::PATTERN_RULEBASED. 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

at line 315
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

at line 354
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

at line 392
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

at line 404
int getErrorCode()

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

Return Value

int The error code from last formatter call

See also

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

at line 416
string getErrorMessage()

Returns formatter's last error message. Always returns the U_ZERO_ERROR_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

at line 433
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::VALID_LOCALE or Locale::ACTUAL_LOCALE)

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

at line 447
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

at line 461
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

at line 475
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

at line 493
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 or false on error

Exceptions

MethodNotImplementedException

See also

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

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

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 or false on error

See also

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

at line 566
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::FRACTION_DIGITS, NumberFormatter::GROUPING_USED 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

at line 616
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

at line 633
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

at line 650
bool setTextAttribute(int $attr, string $value)

Not supported. Set a text attribute.

Parameters

int $attr An attribute specifier, one of the text attribute constants
string $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

at line 658
protected resetError()

Set the error to the default U_ZERO_ERROR.