class NumberFormatter implements FormatterInterface (View source)

Formatter for numbers.

Properties

protected NumbersReader $numbersReader
protected CurrencyReader $currencyReader

Methods

void
injectNumbersReader(NumbersReader $numbersReader)

No description

void
injectCurrencyReader(CurrencyReader $currencyReader)

No description

string
format(mixed $value, Locale $locale, array $styleProperties = [])

Formats provided value using optional style properties

string
formatNumberWithCustomPattern(mixed $number, string $format, Locale $locale)

Returns number formatted by custom format, string provided in parameter.

string
formatDecimalNumber(mixed $number, Locale $locale, string $formatLength = NumbersReader::FORMAT_LENGTH_DEFAULT)

Formats number with format string for decimal numbers defined in CLDR for particular locale.

string
formatPercentNumber(mixed $number, Locale $locale, string $formatLength = NumbersReader::FORMAT_LENGTH_DEFAULT)

Formats number with format string for percentage defined in CLDR for particular locale.

string
formatCurrencyNumber(mixed $number, Locale $locale, string $currency, string $formatLength = NumbersReader::FORMAT_LENGTH_DEFAULT, string $currencyCode = null)

Formats number with format string for currency defined in CLDR for particular locale.

string
doFormattingWithParsedFormat(mixed $number, array $parsedFormat, array $symbols, string $currency = null)

Formats provided float or integer.

Details

void injectNumbersReader(NumbersReader $numbersReader)

No description

Parameters

NumbersReader $numbersReader

Return Value

void

void injectCurrencyReader(CurrencyReader $currencyReader)

No description

Parameters

CurrencyReader $currencyReader

Return Value

void

string format(mixed $value, Locale $locale, array $styleProperties = [])

Formats provided value using optional style properties

Parameters

mixed $value

Formatter-specific variable to format (can be integer, \DateTime, etc)

Locale $locale

Locale to use

array $styleProperties

Integer-indexed array of formatter-specific style properties (can be empty)

Return Value

string

String representation of $value provided, or (string)$value

string formatNumberWithCustomPattern(mixed $number, string $format, Locale $locale)

Returns number formatted by custom format, string provided in parameter.

Format must obey syntax defined in CLDR specification, excluding unimplemented features (see documentation for this class).

Format is remembered in this classes cache and won't be parsed again for some time.

Parameters

mixed $number

Float or int, can be negative, can be NaN or infinite

string $format

Format string

Locale $locale

A locale used for finding symbols array

Return Value

string

Formatted number. Will return string-casted version of $number if pattern is not valid / supported

string formatDecimalNumber(mixed $number, Locale $locale, string $formatLength = NumbersReader::FORMAT_LENGTH_DEFAULT)

Formats number with format string for decimal numbers defined in CLDR for particular locale.

Note: currently length is not used in decimalFormats from CLDR. But it's defined in the specification, so we support it here.

Parameters

mixed $number

Float or int, can be negative, can be NaN or infinite

Locale $locale
string $formatLength

One of NumbersReader FORMAT_LENGTH constants

Return Value

string

Formatted number. Will return string-casted version of $number if there is no pattern for given $locale / $formatLength

string formatPercentNumber(mixed $number, Locale $locale, string $formatLength = NumbersReader::FORMAT_LENGTH_DEFAULT)

Formats number with format string for percentage defined in CLDR for particular locale.

Note: currently length is not used in percentFormats from CLDR. But it's defined in the specification, so we support it here.

Parameters

mixed $number

Float or int, can be negative, can be NaN or infinite

Locale $locale
string $formatLength

One of NumbersReader FORMAT_LENGTH constants

Return Value

string

Formatted number. Will return string-casted version of $number if there is no pattern for given $locale / $formatLength

string formatCurrencyNumber(mixed $number, Locale $locale, string $currency, string $formatLength = NumbersReader::FORMAT_LENGTH_DEFAULT, string $currencyCode = null)

Formats number with format string for currency defined in CLDR for particular locale.

Currency symbol provided will be inserted into formatted number string.

Note: currently length is not used in currencyFormats from CLDR. But it's defined in the specification, so we support it here.

Parameters

mixed $number

Float or int, can be negative, can be NaN or infinite

Locale $locale
string $currency

Currency symbol (or name)

string $formatLength

One of NumbersReader FORMAT_LENGTH constants

string $currencyCode

The ISO 4217 currency code of the currency to format. Used to set decimal places and rounding.

Return Value

string

Formatted number. Will return string-casted version of $number if there is no pattern for given $locale / $formatLength

protected string doFormattingWithParsedFormat(mixed $number, array $parsedFormat, array $symbols, string $currency = null)

Formats provided float or integer.

Format rules defined in $parsedFormat array are used. Localizable symbols are replaced with elements from $symbols array, and currency placeholder is replaced with the value of $currency, if not NULL.

If $number is NaN or infinite, proper localized symbol will be returned, as defined in CLDR specification.

Parameters

mixed $number

Float or int, can be negative, can be NaN or infinite

array $parsedFormat

An array describing format (as in $parsedFormats property)

array $symbols

An array with symbols to use (as in $localeSymbols property)

string $currency

Currency symbol to be inserted into formatted number (if applicable)

Return Value

string

Formatted number. Will return string-casted version of $number if pattern is false