W3cubDocs

/CakePHP 4.1

Class Number

Number helper library.

Methods to make numbers more readable.

Namespace: Cake\I18n

Constants summary

  • int
    CURRENCY_ACCOUNTING
    12
  • string
    DEFAULT_LOCALE
    'en_US'
  • string
    FORMAT_CURRENCY
    'currency'
  • string
    FORMAT_CURRENCY_ACCOUNTING
    'currency_accounting'

Properties summary

  • $_defaultCurrency protected static
    string|null

    Default currency used by Number::currency()

  • $_defaultCurrencyFormat protected static
    string

    Default currency format used by Number::currency()

  • $_formatters protected static
    array

    A list of number formatters indexed by locale and type

Method Summary

  • _setAttributes() protected static

    Set formatter attributes

  • config() public static

    Configure formatters.

  • currency() public static

    Formats a number into a currency format.

  • defaultCurrency() public static

    Getter/setter for default currency. This behavior is deprecated and will be removed in future versions of CakePHP.

  • format() public static

    Formats a number into the correct locale format

  • formatDelta() public static

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

  • formatter() public static

    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.

  • getDefaultCurrency() public static

    Getter for default currency

  • getDefaultCurrencyFormat() public static

    Getter for default currency format

  • ordinal() public static

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

  • parseFloat() public static

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

  • precision() public static

    Formats a number with a level of precision.

  • setDefaultCurrency() public static

    Setter for default currency

  • setDefaultCurrencyFormat() public static

    Setter for default currency format

  • toPercentage() public static

    Formats a number into a percentage string.

  • toReadableSize() public static

    Returns a formatted-for-humans file size.

Method Detail

_setAttributes() protected static

_setAttributes(\NumberFormatter $formatter, array $options)

Set formatter attributes

Parameters

\NumberFormatter $formatter

Number formatter instance.

array $options optional

See Number::formatter() for possible options.

Returns

\NumberFormatter

config() public static

config(string $locale, int $type, array $options)

Configure formatters.

Parameters

string $locale

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

int $type optional

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

array $options optional

See Number::formatter() for possible options.

currency() public static

currency(mixed $value, ?string $currency, array $options)

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 #,##0.00
  • useIntlCode - Whether or not to replace the currency symbol with the international currency code.

Parameters

float|string $value

Value to format.

string|null $currency optional

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

array $options optional

Options list.

Returns

string

Number formatted as a currency.

defaultCurrency() public static

defaultCurrency(mixed $currency)

Getter/setter for default currency. This behavior is deprecated and will be removed in future versions of CakePHP.

Parameters

string|false|null $currency optional

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

Returns

string|null

Currency

format() public static

format(mixed $value, array $options)

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 #,##0.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|string $value

A floating point number.

array $options optional

An array with options.

Returns

string

Formatted number

formatDelta() public static

formatDelta(mixed $value, array $options)

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

A floating point number

array $options optional

Options list.

Returns

string

formatted delta

formatter() public static

formatter(array $options)

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 #,##0.00
  • useIntlCode - Whether or not to replace the currency symbol with the international currency code.

Parameters

array $options optional

An array with options.

Returns

\NumberFormatter

The configured formatter instance

getDefaultCurrency() public static

getDefaultCurrency()

Getter for default currency

Returns

string

Currency

getDefaultCurrencyFormat() public static

getDefaultCurrencyFormat()

Getter for default currency format

Returns

string

Currency Format

ordinal() public static

ordinal(mixed $value, array $options)

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

int|float $value

An integer

array $options optional

An array with options.

Returns

string

parseFloat() public static

parseFloat(string $value, array $options)

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 optional

An array with options.

Returns

float

point number

precision() public static

precision(mixed $value, int $precision, array $options)

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

A floating point number.

int $precision optional

The precision of the returned number.

array $options optional

Additional options

Returns

string

Formatted float.

Links

setDefaultCurrency() public static

setDefaultCurrency(?string $currency)

Setter for default currency

Parameters

string|null $currency optional

Default currency string to be used by {@link currency()} if $currency argument is not provided. If null is passed, it will clear the currently stored value

setDefaultCurrencyFormat() public static

setDefaultCurrencyFormat(mixed $currencyFormat)

Setter for default currency format

Parameters

string|null $currencyFormat optional

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

toPercentage() public static

toPercentage(mixed $value, int $precision, array $options)

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

A floating point number

int $precision optional

The precision of the returned number

array $options optional

Options

Returns

string

Percentage string

Links

toReadableSize() public static

toReadableSize(mixed $size)

Returns a formatted-for-humans file size.

Parameters

int|string $size

Size in bytes

Returns

string

Human readable size

Links

Property Detail

$_defaultCurrency protected static

Default currency used by Number::currency()

Type

string|null

$_defaultCurrencyFormat protected static

Default currency format used by Number::currency()

Type

string

$_formatters protected static

A list of number formatters indexed by locale and type

Type

array

© 2005–present The Cake Software Foundation, Inc.
Licensed under the MIT License.
CakePHP is a registered trademark of Cake Software Foundation, Inc.
We are not endorsed by or affiliated with CakePHP.
https://api.cakephp.org/4.1/class-Cake.I18n.Number.html