\Cake\I18nNumber

Number helper library.

Methods to make numbers more readable.

Summary

Methods
Properties
Constants
precision()
toReadableSize()
toPercentage()
format()
parseFloat()
formatDelta()
currency()
defaultCurrency()
formatter()
config()
ordinal()
No public properties found
DEFAULT_LOCALE
FORMAT_CURRENCY
_setAttributes()
$_formatters
$_defaultCurrency
N/A
No private methods found
No private properties found
N/A

Constants

DEFAULT_LOCALE

DEFAULT_LOCALE = 'en_US' : string

Default locale

FORMAT_CURRENCY

FORMAT_CURRENCY = 'currency' : string

Format type to format as currency

Properties

$_formatters

$_formatters : array

A list of number formatters indexed by locale and type

Type

array

$_defaultCurrency

$_defaultCurrency : string|null

Default currency used by Number::currency()

Type

string|null

Methods

precision()

precision(float  $value, integer  $precision = 3, array  $options = array()) : string

Formats a number with a level of precision.

Options:

  • locale: The locale name to use for formatting the number, e.g. fr_FR

Parameters

float $value

A floating point number.

integer $precision

The precision of the returned number.

array $options

Additional options

Returns

string —

Formatted float.

toReadableSize()

toReadableSize(integer  $size) : string

Returns a formatted-for-humans file size.

Parameters

integer $size

Size in bytes

Returns

string —

Human readable size

toPercentage()

toPercentage(float  $value, integer  $precision = 2, array  $options = array()) : string

Formats a number into a percentage string.

Options:

  • multiply: Multiply the input value by 100 for decimal percentages.
  • locale: The locale name to use for formatting the number, e.g. fr_FR

Parameters

float $value

A floating point number

integer $precision

The precision of the returned number

array $options

Options

Returns

string —

Percentage string

format()

format(float  $value, array  $options = array()) : string

Formats a number into the correct locale format

Options:

  • places - Minimum number or decimals to use, e.g 0
  • precision - Maximum Number of decimal places to use, e.g. 2
  • pattern - An ICU number pattern to use for formatting the number. e.g #,###.00
  • locale - The locale name to use for formatting the number, e.g. fr_FR
  • before - The string to place before whole numbers, e.g. '['
  • after - The string to place after decimal numbers, e.g. ']'

Parameters

float $value

A floating point number.

array $options

An array with options.

Returns

string —

Formatted number

parseFloat()

parseFloat(string  $value, array  $options = array()) : float

Parse a localized numeric string and transform it in a float point

Options:

  • locale - The locale name to use for parsing the number, e.g. fr_FR
  • type - The formatter type to construct, set it to currency if you need to parse numbers representing money.

Parameters

string $value

A numeric string.

array $options

An array with options.

Returns

float —

point number

formatDelta()

formatDelta(float  $value, array  $options = array()) : string

Formats a number into the correct locale format to show deltas (signed differences in value).

Options

  • places - Minimum number or decimals to use, e.g 0
  • precision - Maximum Number of decimal places to use, e.g. 2
  • locale - The locale name to use for formatting the number, e.g. fr_FR
  • before - The string to place before whole numbers, e.g. '['
  • after - The string to place after decimal numbers, e.g. ']'

Parameters

float $value

A floating point number

array $options

Options list.

Returns

string —

formatted delta

currency()

currency(float  $value, string|null  $currency = null, array  $options = array()) : string

Formats a number into a currency format.

Options

  • locale - The locale name to use for formatting the number, e.g. fr_FR
  • fractionSymbol - The currency symbol to use for fractional numbers.
  • fractionPosition - The position the fraction symbol should be placed valid options are 'before' & 'after'.
  • before - Text to display before the rendered number
  • after - Text to display after the rendered number
  • zero - The text to use for zero values, can be a string or a number. e.g. 0, 'Free!'
  • places - Number of decimal places to use. e.g. 2
  • precision - Maximum Number of decimal places to use, e.g. 2
  • pattern - An ICU number pattern to use for formatting the number. e.g #,###.00
  • useIntlCode - Whether or not to replace the currency symbol with the international currency code.

Parameters

float $value

Value to format.

string|null $currency

International currency name such as 'USD', 'EUR', 'JPY', 'CAD'

array $options

Options list.

Returns

string —

Number formatted as a currency.

defaultCurrency()

defaultCurrency(string|boolean|null  $currency = null) : string|null

Getter/setter for default currency

Parameters

string|boolean|null $currency

Default currency string to be used by currency() if $currency argument is not provided. If boolean false is passed, it will clear the currently stored value

Returns

string|null —

Currency

formatter()

formatter(array  $options = array()) : \NumberFormatter

Returns a formatter object that can be reused for similar formatting task under the same locale and options. This is often a speedier alternative to using other methods in this class as only one formatter object needs to be constructed.

Options

  • locale - The locale name to use for formatting the number, e.g. fr_FR
  • type - The formatter type to construct, set it to currency if you need to format numbers representing money or a NumberFormatter constant.
  • places - Number of decimal places to use. e.g. 2
  • precision - Maximum Number of decimal places to use, e.g. 2
  • pattern - An ICU number pattern to use for formatting the number. e.g #,###.00
  • useIntlCode - Whether or not to replace the currency symbol with the international currency code.

Parameters

array $options

An array with options.

Returns

\NumberFormatter —

The configured formatter instance

config()

config(string  $locale, integer  $type = \NumberFormatter::DECIMAL, array  $options = array()) : void

Configure formatters.

Parameters

string $locale

The locale name to use for formatting the number, e.g. fr_FR

integer $type

The formatter type to construct. Defaults to NumberFormatter::DECIMAL.

array $options

See Number::formatter() for possible options.

ordinal()

ordinal(integer|float  $value, array  $options = array()) : string

Returns a formatted integer as an ordinal number string (e.g. 1st, 2nd, 3rd, 4th, [.

..])

Options

  • type - The formatter type to construct, set it to currency if you need to format numbers representing money or a NumberFormatter constant.

For all other options see formatter().

Parameters

integer|float $value

An integer

array $options

An array with options.

Returns

string

_setAttributes()

_setAttributes(\NumberFormatter  $formatter, array  $options = array()) : \NumberFormatter

Set formatter attributes

Parameters

\NumberFormatter $formatter

Number formatter instance.

array $options

See Number::formatter() for possible options.

Returns

\NumberFormatter