Class CakeNumber
Number helper library.
Methods to make numbers more readable.
Link: https://book.cakephp.org/2.0/en/core-libraries/helpers/number.html
Copyright: Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
License: MIT License
Location: Cake/Utility/CakeNumber.php
Properties summary
-
array
Currencies supported by the helper. You can add additional currency formats with CakeNumber::addFormat
- Default options for currency formats
array
- Default currency used by CakeNumber::currency()
string
- If native number_format() should be used. If >= PHP5.4
boolean
Method Summary
- Alternative number_format() to accommodate multibyte decimals and thousands < PHP 5.4
-
Add a currency format to the Number helper. Makes reusing currency formats easier.
- Formats a number into a currency format.
- Getter/setter for default currency
- Formats a number into a currency format.
- Formats a number into a currency format to show deltas (signed differences in value).
- Converts filesize from human readable string to bytes
- Formats a number with a level of precision.
- Formats a number into a percentage string.
- Returns a formatted-for-humans file size.
Method Detail
_numberFormat()source protected static
_numberFormat( float $value , integer $places 0 , string $decimals '.' , string $thousands ',' )
Alternative number_format() to accommodate multibyte decimals and thousands < PHP 5.4
Parameters
- float
$value
- Value to format.
- integer
$places
optional 0 - Decimal places to use.
- string
$decimals
optional '.' - Decimal position string.
- string
$thousands
optional ',' - Thousands separator string.
Returns
stringaddFormat()source public static
addFormat( string $formatName , array $options )
Add a currency format to the Number helper. Makes reusing currency formats easier.
$number->addFormat('NOK', array('before' => 'Kr. ')); ``` You can now use `NOK` as a shortform when formatting currency amounts.
$number->currency($value, 'NOK'); ```
Added formats are merged with the defaults defined in CakeNumber::$_currencyDefaults See CakeNumber::currency() for more information on the various options and their function.
Parameters
- string
$formatName
- The format name to be used in the future.
- array
$options
- The array of options for this format.
See
NumberHelper::currency()Link
https://book.cakephp.org/2.0/en/core-libraries/helpers/number.html#NumberHelper::addFormatcurrency()source public static
currency( float $value , string $currency null , array $options array() )
Formats a number into a currency format.
Options
-
wholeSymbol
- The currency symbol to use for whole numbers, greater than 1, or less than -1. -
wholePosition
- The position the whole symbol should be placed valid options are 'before' & 'after'. -
fractionSymbol
- The currency symbol to use for fractional numbers. -
fractionPosition
- The position the fraction symbol should be placed valid options are 'before' & 'after'. -
before
- The currency symbol to place before whole numbers ie. '$'.before
is an alias forwholeSymbol
. -
after
- The currency symbol to place after decimal numbers ie. 'c'. Set to boolean false to use no decimal symbol. eg. 0.35 => $0.35.after
is an alias forfractionSymbol
-
zero
- The text to use for zero values, can be a string or a number. ie. 0, 'Free!' -
places
- Number of decimal places to use. ie. 2 -
fractionExponent
- Fraction exponent of this specific currency. Defaults to 2. -
thousands
- Thousands separator ie. ',' -
decimals
- Decimal separator symbol ie. '.' -
negative
- Symbol for negative numbers. If equal to '()', the number will be wrapped with ( and ) -
escape
- Should the output be escaped for html special characters. The default value for this option is controlled by the currency settings. By default all currencies contain utf-8 symbols and don't need this changed. If you require non HTML encoded symbols you will need to update the settings with the correct bytes.
Parameters
- float
$value
- Value to format.
- string
$currency
optional null Shortcut to default options. Valid values are 'USD', 'EUR', 'GBP', otherwise set at least 'before' and 'after' options.
- array
$options
optional array() - Options list.
Returns
stringNumber formatted as a currency.
Link
https://book.cakephp.org/2.0/en/core-libraries/helpers/number.html#NumberHelper::currencydefaultCurrency()source public static
defaultCurrency( string $currency null )
Getter/setter for default currency
Parameters
- string
$currency
optional null - Default currency string used by currency() if $currency argument is not provided
Returns
stringCurrency
Link
https://book.cakephp.org/2.0/en/core-libraries/helpers/number.html#NumberHelper::defaultCurrencyformat()source public static
format( float $value , integer $options false )
Formats a number into a currency format.
Parameters
- float
$value
- A floating point number
- integer
$options
optional false If integer then places, if string then before, if (,.-) then use it or array with places and before keys
Returns
stringformatted number
Link
https://book.cakephp.org/2.0/en/core-libraries/helpers/number.html#NumberHelper::formatformatDelta()source public static
formatDelta( float $value , array $options array() )
Formats a number into a currency format to show deltas (signed differences in value).
Options
-
places
- Number of decimal places to use. ie. 2 -
fractionExponent
- Fraction exponent of this specific currency. Defaults to 2. -
before
- The string to place before whole numbers. ie. '[' -
after
- The string to place after decimal numbers. ie. ']' -
thousands
- Thousands separator ie. ',' -
decimals
- Decimal separator symbol ie. '.'
Parameters
- float
$value
- A floating point number
- array
$options
optional array() - Options list.
Returns
stringformatted delta
Link
https://book.cakephp.org/2.0/en/core-libraries/helpers/number.html#NumberHelper::formatDeltafromReadableSize()source public static
fromReadableSize( string $size , mixed $default false )
Converts filesize from human readable string to bytes
Parameters
- string
$size
- Size in human readable string like '5MB', '5M', '500B', '50kb' etc.
- mixed
$default
optional false - Value to be returned when invalid size was used, for example 'Unknown type'
Returns
mixedNumber of bytes as integer on success,
$default
on failure if not falseThrows
CakeException
On invalid Unit type.
Link
https://book.cakephp.org/2.0/en/core-libraries/helpers/number.html#NumberHelper::fromReadableSizeprecision()source public static
precision( float $value , integer $precision 3 )
Formats a number with a level of precision.
Parameters
- float
$value
- A floating point number.
- integer
$precision
optional 3 - The precision of the returned number.
Returns
floatFormatted float.
Link
https://book.cakephp.org/2.0/en/core-libraries/helpers/number.html#NumberHelper::precisiontoPercentage()source public static
toPercentage( float $value , integer $precision 2 , array $options array() )
Formats a number into a percentage string.
Options:
-
multiply
: Multiply the input value by 100 for decimal percentages.
Parameters
- float
$value
- A floating point number
- integer
$precision
optional 2 - The precision of the returned number
- array
$options
optional array() - Options
Returns
stringPercentage string
Link
https://book.cakephp.org/2.0/en/core-libraries/helpers/number.html#NumberHelper::toPercentagetoReadableSize()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/2.0/en/core-libraries/helpers/number.html#NumberHelper::toReadableSizeProperties detail
$_currenciessource
protected static array
Currencies supported by the helper. You can add additional currency formats with CakeNumber::addFormat
array( 'AUD' => array( 'wholeSymbol' => '$', 'wholePosition' => 'before', 'fractionSymbol' => 'c', 'fractionPosition' => 'after', 'zero' => 0, 'places' => 2, 'thousands' => ',', 'decimals' => '.', 'negative' => '()', 'escape' => true, 'fractionExponent' => 2 ), 'CAD' => array( 'wholeSymbol' => '$', 'wholePosition' => 'before', 'fractionSymbol' => 'c', 'fractionPosition' => 'after', 'zero' => 0, 'places' => 2, 'thousands' => ',', 'decimals' => '.', 'negative' => '()', 'escape' => true, 'fractionExponent' => 2 ), 'USD' => array( 'wholeSymbol' => '$', 'wholePosition' => 'before', 'fractionSymbol' => 'c', 'fractionPosition' => 'after', 'zero' => 0, 'places' => 2, 'thousands' => ',', 'decimals' => '.', 'negative' => '()', 'escape' => true, 'fractionExponent' => 2 ), 'EUR' => array( 'wholeSymbol' => '€', 'wholePosition' => 'before', 'fractionSymbol' => false, 'fractionPosition' => 'after', 'zero' => 0, 'places' => 2, 'thousands' => '.', 'decimals' => ',', 'negative' => '()', 'escape' => true, 'fractionExponent' => 0 ), 'GBP' => array( 'wholeSymbol' => '£', 'wholePosition' => 'before', 'fractionSymbol' => 'p', 'fractionPosition' => 'after', 'zero' => 0, 'places' => 2, 'thousands' => ',', 'decimals' => '.', 'negative' => '()', 'escape' => true, 'fractionExponent' => 2 ), 'JPY' => array( 'wholeSymbol' => '¥', 'wholePosition' => 'before', 'fractionSymbol' => false, 'fractionPosition' => 'after', 'zero' => 0, 'places' => 2, 'thousands' => ',', 'decimals' => '.', 'negative' => '()', 'escape' => true, 'fractionExponent' => 0 ), )
$_currencyDefaultssource
protected static array
Default options for currency formats
array( 'wholeSymbol' => '', 'wholePosition' => 'before', 'fractionSymbol' => false, 'fractionPosition' => 'after', 'zero' => '0', 'places' => 2, 'thousands' => ',', 'decimals' => '.', 'negative' => '()', 'escape' => true, 'fractionExponent' => 2 )
$_defaultCurrencysource
protected static string
Default currency used by CakeNumber::currency()
'USD'
$_numberFormatSupportsource
protected static boolean
If native number_format() should be used. If >= PHP5.4
null
© 2005–2017 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/2.10/class-CakeNumber.html