Fr3nch13/CakePHP Utilities

Number
in package

Number helper library.

Methods to make numbers more readable.

Tags
link
https://book.cakephp.org/4/en/core-libraries/number.html

Table of Contents

Constants

CURRENCY_ACCOUNTING  = 12
ICU Constant for accounting format; not yet widely supported by INTL library.
DEFAULT_LOCALE  = 'en_US'
Default locale
FORMAT_CURRENCY  = 'currency'
Format type to format as currency
FORMAT_CURRENCY_ACCOUNTING  = 'currency_accounting'
Format type to format as currency, accounting style (negative numbers in parentheses)

Properties

$_defaultCurrency  : string|null
Default currency used by Number::currency()
$_defaultCurrencyFormat  : string|null
Default currency format used by Number::currency()
$_formatters  : array<string, array<int, mixed>>
A list of number formatters indexed by locale and type

Methods

config()  : void
Configure formatters.
currency()  : string
Formats a number into a currency format.
defaultCurrency()  : string|null
Getter/setter for default currency. This behavior is *deprecated* and will be removed in future versions of CakePHP.
format()  : string
Formats a number into the correct locale format
formatDelta()  : string
Formats a number into the correct locale format to show deltas (signed differences in value).
formatter()  : 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.
getDefaultCurrency()  : string
Getter for default currency
getDefaultCurrencyFormat()  : string
Getter for default currency format
ordinal()  : string
Returns a formatted integer as an ordinal number string (e.g. 1st, 2nd, 3rd, 4th, [...])
parseFloat()  : float
Parse a localized numeric string and transform it in a float point
precision()  : string
Formats a number with a level of precision.
setDefaultCurrency()  : void
Setter for default currency
setDefaultCurrencyFormat()  : void
Setter for default currency format
toPercentage()  : string
Formats a number into a percentage string.
toReadableSize()  : string
Returns a formatted-for-humans file size.
_setAttributes()  : NumberFormatter
Set formatter attributes

Constants

CURRENCY_ACCOUNTING

ICU Constant for accounting format; not yet widely supported by INTL library.

public int CURRENCY_ACCOUNTING = 12

This will be able to go away once CakePHP minimum PHP requirement is 7.4.1 or higher. See UNUM_CURRENCY_ACCOUNTING in https://unicode-org.github.io/icu-docs/apidoc/released/icu4c/unum_8h.html

DEFAULT_LOCALE

Default locale

public string DEFAULT_LOCALE = 'en_US'

FORMAT_CURRENCY

Format type to format as currency

public string FORMAT_CURRENCY = 'currency'

FORMAT_CURRENCY_ACCOUNTING

Format type to format as currency, accounting style (negative numbers in parentheses)

public string FORMAT_CURRENCY_ACCOUNTING = 'currency_accounting'

Properties

$_defaultCurrency

Default currency used by Number::currency()

protected static string|null $_defaultCurrency

$_defaultCurrencyFormat

Default currency format used by Number::currency()

protected static string|null $_defaultCurrencyFormat

$_formatters

A list of number formatters indexed by locale and type

protected static array<string, array<int, mixed>> $_formatters = []

Methods

config()

Configure formatters.

public static config(string $locale[, int $type = NumberFormatter::DECIMAL ][, array<string, mixed> $options = [] ]) : void
Parameters
$locale : string

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

$type : int = NumberFormatter::DECIMAL

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

$options : array<string, mixed> = []

See Number::formatter() for possible options.

currency()

Formats a number into a currency format.

public static currency(string|float $value[, string|null $currency = null ][, array<string, mixed> $options = [] ]) : string

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 to replace the currency symbol with the international currency code.
Parameters
$value : string|float

Value to format.

$currency : string|null = null

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

$options : array<string, mixed> = []

Options list.

Return values
string

Number formatted as a currency.

defaultCurrency()

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

public static defaultCurrency([string|false|null $currency = null ]) : string|null

Use and instead.

Parameters
$currency : string|false|null = null

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

Return values
string|null

Currency

format()

Formats a number into the correct locale format

public static format(string|int|float $value[, array<string, mixed> $options = [] ]) : string

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
$value : string|int|float

A floating point number.

$options : array<string, mixed> = []

An array with options.

Return values
string

Formatted number

formatDelta()

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

public static formatDelta(string|float $value[, array<string, mixed> $options = [] ]) : string

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

A floating point number

$options : array<string, mixed> = []

Options list.

Return values
string

formatted delta

formatter()

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.

public static formatter([array<string, mixed> $options = [] ]) : NumberFormatter

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 to replace the currency symbol with the international currency code.
Parameters
$options : array<string, mixed> = []

An array with options.

Return values
NumberFormatter

The configured formatter instance

getDefaultCurrency()

Getter for default currency

public static getDefaultCurrency() : string
Return values
string

Currency

getDefaultCurrencyFormat()

Getter for default currency format

public static getDefaultCurrencyFormat() : string
Return values
string

Currency Format

ordinal()

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

public static ordinal(float|int $value[, array<string, mixed> $options = [] ]) : string

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
$value : float|int

An integer

$options : array<string, mixed> = []

An array with options.

Return values
string

parseFloat()

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

public static parseFloat(string $value[, array<string, mixed> $options = [] ]) : float

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

A numeric string.

$options : array<string, mixed> = []

An array with options.

Return values
float

point number

precision()

Formats a number with a level of precision.

public static precision(string|float $value[, int $precision = 3 ][, array<string, mixed> $options = [] ]) : string

Options:

  • locale: The locale name to use for formatting the number, e.g. fr_FR
Parameters
$value : string|float

A floating point number.

$precision : int = 3

The precision of the returned number.

$options : array<string, mixed> = []

Additional options

Tags
link
https://book.cakephp.org/4/en/core-libraries/number.html#formatting-floating-point-numbers
Return values
string

Formatted float.

setDefaultCurrency()

Setter for default currency

public static setDefaultCurrency([string|null $currency = null ]) : void
Parameters
$currency : string|null = null

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

setDefaultCurrencyFormat()

Setter for default currency format

public static setDefaultCurrencyFormat([string|null $currencyFormat = null ]) : void
Parameters
$currencyFormat : string|null = null

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()

Formats a number into a percentage string.

public static toPercentage(string|float $value[, int $precision = 2 ][, array<string, mixed> $options = [] ]) : 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
$value : string|float

A floating point number

$precision : int = 2

The precision of the returned number

$options : array<string, mixed> = []

Options

Tags
link
https://book.cakephp.org/4/en/core-libraries/number.html#formatting-percentages
Return values
string

Percentage string

_setAttributes()

Set formatter attributes

protected static _setAttributes(NumberFormatter $formatter[, array<string, mixed> $options = [] ]) : NumberFormatter
Parameters
$formatter : NumberFormatter

Number formatter instance.

$options : array<string, mixed> = []

See Number::formatter() for possible options.

Return values
NumberFormatter

        
On this page

Search results