Number
Cake\I18n\Number::
currency
(mixed $value, string $currency = null, array $options = [])- This method is used to display a number in common currency formats(EUR, GBP, USD). Usage in a view looks like:
- // Called as NumberHelper
- echo $this->Number->currency($value, $currency);
- // Called as Number
- echo Number::currency($value, $currency);
The first parameter, $value
, should be a floating point numberthat represents the amount of money you are expressing. The secondparameter is a string used to choose a predefined currency formattingscheme:
The third parameter is an array of options for further defining theoutput. The following options are available:
If $currency value is null
, the default currency will be retrieved fromCake\I18n\Number::defaultCurrency()
Setting the Default Currency
Cake\I18n\Number::
defaultCurrency
($currency)- Setter/getter for the default currency. This removes the need to always pass thecurrency to and change allcurrency outputs by setting other default. If
$currency
is set tofalse
,it will clear the currently stored value. By default, it will retrieve theintl.default_locale
if set and ‘en_US’ if not.
Cake\I18n\Number::
precision
(float $value, int $precision = 3, array $options = [])- This method displays a number with the specified amount ofprecision (decimal places). It will round in order to maintain thelevel of precision defined.
- // Called as NumberHelper
- echo $this->Number->precision(456.91873645, 2);
- // Outputs
- 456.92
- // Called as Number
- echo Number::precision(456.91873645, 2);
Formatting Percentages
Cake\I18n\Number::
toReadableSize
(string $size)- This method formats data sizes in human readable forms. It providesa shortcut way to convert bytes to KB, MB, GB, and TB. The size isdisplayed with a two-digit precision level, according to the sizeof data supplied (i.e. higher sizes are expressed in largerterms):
- // Called as NumberHelper
- echo $this->Number->toReadableSize(0); // 0 Byte
- echo $this->Number->toReadableSize(1024); // 1 KB
- echo $this->Number->toReadableSize(1321205.76); // 1.26 MB
- echo $this->Number->toReadableSize(5368709120); // 5 GB
- // Called as Number
- echo Number::toReadableSize(0); // 0 Byte
- echo Number::toReadableSize(1024); // 1 KB
- echo Number::toReadableSize(1321205.76); // 1.26 MB
- echo Number::toReadableSize(5368709120); // 5 GB
Formatting Numbers
Cake\I18n\Number::
format
(mixed $value, array $options = [])- This method gives you much more control over the formatting ofnumbers for use in your views (and is used as the main method bymost of the other NumberHelper methods). Using this method mightlooks like:
- // Called as NumberHelper
- $this->Number->format($value, $options);
- // Called as Number
- Number::format($value, $options);
The parameter is the number that you are planning onformatting for output. With no $options
supplied, the number1236.334 would output as 1,236. Note that the default precision iszero decimal places.
The $options
parameter is where the real magic for this methodresides.
- If you pass an integer then this becomes the amount of precisionor places for the function.
- If you pass an associated array, you can use the following keys:
Example:
- This method will output an ordinal number.
- echo Number::ordinal(1);
- // Output '1st'
- echo Number::ordinal(2);
- // Output '2nd'
- echo Number::ordinal(2, [
- 'locale' => 'fr_FR'
- ]);
- // Output '2e'
- echo Number::ordinal(410);
- // Output '410th'
Cake\I18n\Number::
formatDelta
(mixed $value, array $options = [])- This method displays differences in value as a signed number:
- // Called as NumberHelper
- $this->Number->formatDelta($value, $options);
- // Called as Number
- Number::formatDelta($value, $options);
The $value
parameter is the number that you are planning onformatting for output. With no $options
supplied, the number1236.334 would output as 1,236. Note that the default precision iszero decimal places.
The $options
parameter takes the same keys as itself:
Example:
Configure formatters
Cake\I18n\Number::
config
(string $locale, int $type = NumberFormatter::DECIMAL, array $options = [])- This method allows you to configure formatter defaults which persist across callsto various methods.
- Number::config('en_IN', \NumberFormatter::CURRENCY, [
- 'pattern' => '#,##,##0'
- ]);