On this page
Class Number
Number helper library.
Methods to make numbers more readable.
Link: https://book.cakephp.org/3.0/en/core-libraries/number.html
Location: I18n/Number.php
Constants summary
stringDEFAULT_LOCALE'en_US'stringFORMAT_CURRENCY'currency'
Properties summary
-
Default currency used by Number::currency()
string|null -
A list of number formatters indexed by locale and type
array
Method Summary
- Set formatter attributes
- Configure formatters.
- Formats a number into a currency format.
- Getter/setter for default currency
- Formats a number into the correct locale format
- Formats a number into the correct locale format to show deltas (signed differences in value).
-
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.
- Returns a formatted integer as an ordinal number string (e.g. 1st, 2nd, 3rd, 4th, [...])
- Parse a localized numeric string and transform it in a float point
- Formats a number with a level of precision.
- Formats a number into a percentage string.
- Returns a formatted-for-humans file size.
Method Detail
_setAttributes()source protected static
_setAttributes( NumberFormatter $formatter , array $options = [] )
Set formatter attributes
Parameters
-
NumberFormatter
$formatter - Number formatter instance.
-
array
$optionsoptional [] - See Number::formatter() for possible options.
Returns
NumberFormatterconfig()source public static
config( string $locale , integer $type = Cake\I18n\NumberFormatter::DECIMAL , array $options = [] )
Configure formatters.
Parameters
-
string
$locale - The locale name to use for formatting the number, e.g. fr_FR
-
integer
$typeoptional Cake\I18n\NumberFormatter::DECIMAL - The formatter type to construct. Defaults to NumberFormatter::DECIMAL.
-
array
$optionsoptional [] - See Number::formatter() for possible options.
currency()source public static
currency( float $value , string|null $currency = null , array $options = [] )
Formats a number into a currency format.
Options
locale- The locale name to use for formatting the number, e.g. fr_FRfractionSymbol- 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 numberafter- Text to display after the rendered numberzero- 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. 2precision- Maximum Number of decimal places to use, e.g. 2pattern- An ICU number pattern to use for formatting the number. e.g #,##0.00useIntlCode- Whether or not to replace the currency symbol with the international currency code.
Parameters
-
float
$value - Value to format.
-
string|null
$currencyoptional null - International currency name such as 'USD', 'EUR', 'JPY', 'CAD'
-
array
$optionsoptional [] - Options list.
Returns
stringNumber formatted as a currency.
defaultCurrency()source public static
defaultCurrency( string|boolean|null $currency = null )
Getter/setter for default currency
Parameters
-
string|boolean|null
$currencyoptional null -
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|nullCurrency
format()source public static
format( float $value , array $options = [] )
Formats a number into the correct locale format
Options:
places- Minimum number or decimals to use, e.g 0precision- Maximum Number of decimal places to use, e.g. 2pattern- An ICU number pattern to use for formatting the number. e.g #,##0.00locale- The locale name to use for formatting the number, e.g. fr_FRbefore- 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
$optionsoptional [] - An array with options.
Returns
stringFormatted number
formatDelta()source public static
formatDelta( float $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 0precision- Maximum Number of decimal places to use, e.g. 2locale- The locale name to use for formatting the number, e.g. fr_FRbefore- 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
$optionsoptional [] - Options list.
Returns
stringformatted delta
formatter()source 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_FRtype- The formatter type to construct, set it tocurrencyif you need to format numbers representing money or a NumberFormatter constant.places- Number of decimal places to use. e.g. 2precision- Maximum Number of decimal places to use, e.g. 2pattern- An ICU number pattern to use for formatting the number. e.g #,##0.00useIntlCode- Whether or not to replace the currency symbol with the international currency code.
Parameters
-
array
$optionsoptional [] - An array with options.
Returns
NumberFormatterThe configured formatter instance
ordinal()source public static
ordinal( integer|float $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 tocurrencyif you need to format numbers representing money or a NumberFormatter constant.
For all other options see formatter().
Parameters
-
integer|float
$value - An integer
-
array
$optionsoptional [] - An array with options.
Returns
stringparseFloat()source 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_FRtype- The formatter type to construct, set it tocurrencyif you need to parse numbers representing money.
Parameters
-
string
$value - A numeric string.
-
array
$optionsoptional [] - An array with options.
Returns
floatpoint number
precision()source public static
precision( float $value , integer $precision = 3 , 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
$value - A floating point number.
-
integer
$precisionoptional 3 - The precision of the returned number.
-
array
$optionsoptional [] - Additional options
Returns
stringFormatted float.
Link
https://book.cakephp.org/3.0/en/core-libraries/number.html#formatting-floating-point-numberstoPercentage()source public static
toPercentage( float $value , integer $precision = 2 , 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
$value - A floating point number
-
integer
$precisionoptional 2 - The precision of the returned number
-
array
$optionsoptional [] - Options
Returns
stringPercentage string
Link
https://book.cakephp.org/3.0/en/core-libraries/number.html#formatting-percentagestoReadableSize()source public static
toReadableSize( integer $size )
Returns a formatted-for-humans file size.
Parameters
-
integer
$size - Size in bytes
Returns
stringHuman readable size
Link
https://book.cakephp.org/3.0/en/core-libraries/number.html#interacting-with-human-readable-valuesProperties detail
© 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/3.7/class-Cake.I18n.Number.html