The NumberHelper contains convenience methods that enable display numbers in common formats in your views. These methods include ways to format currency, percentages, data sizes, format numbers to specific precisions and also to give you more flexibility with formatting numbers.
Changed in version 2.1: NumberHelper have been refactored into CakeNumber class to allow easier use outside of the View layer. Within a view, these methods are accessible via the NumberHelper class and you can call it as you would call a normal helper method: $this->Number->method($args);.
All of these functions return the formatted number; They do not automatically echo the output into the view.
Parameters: |
|
---|
This method is used to display a number in common currency formats (EUR,GBP,USD). Usage in a view looks like:
<?php
// called as NumberHelper
echo $this->Number->currency($number, $currency);
// called as CakeNumber
App::uses('CakeNumber', 'Utility');
echo CakeNumber::currency($number, $currency);
The first parameter, $number, should be a floating point number that represents the amount of money you are expressing. The second parameter is used to choose a predefined currency formatting scheme:
$currency | 1234.56, formatted by currency type |
---|---|
EUR | € 1.236,33 |
GBP | £ 1,236.33 |
USD | $ 1,236.33 |
The third parameter is an array of options for further defining the output. The following options are available:
Option | Description |
---|---|
before | The currency symbol to place before whole numbers ie. ‘$’ |
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. |
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 |
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 htmlentity escaped? Defaults to true |
wholeSymbol | String to use for whole numbers ie. ‘ dollars’ |
wholePosition | Either ‘before’ or ‘after’ to place the whole symbol |
fractionSymbol | String to use for fraction numbers ie. ‘ cents’ |
fractionPosition | Either ‘before’ or ‘after’ to place the fraction symbol |
If a non-recognized $currency value is supplied, it is prepended to a USD formatted number. For example:
<?php
// called as NumberHelper
echo $this->Number->currency('1234.56', 'FOO');
// Outputs
FOO 1,234.56
// called as CakeNumber
App::uses('CakeNumber', 'Utility');
echo CakeNumber::currency('1234.56', 'FOO');
Parameters: |
|
---|
Add a currency format to the Number helper. Makes reusing currency formats easier:
<?php
// called as NumberHelper
$this->Number->addFormat('BRR', array('before' => 'R$ '));
// called as CakeNumber
App::uses('CakeNumber', 'Utility');
CakeNumber::addFormat('BRR', array('before' => 'R$ '));
You can now use BRR as a short form when formatting currency amounts:
<?php
// called as NumberHelper
echo $this->Number->currency($value, 'BRR');
// called as CakeNumber
App::uses('CakeNumber', 'Utility');
echo CakeNumber::currency($value, 'BRR');
Added formats are merged with the following defaults:
<?php
array(
'wholeSymbol' => '',
'wholePosition' => 'before',
'fractionSymbol' => '',
'fractionPosition' => 'after',
'zero' => 0,
'places' => 2,
'thousands' => ',',
'decimals' => '.',
'negative' => '()',
'escape' => true
)
Parameters: |
|
---|
This method displays a number with the specified amount of precision (decimal places). It will round in order to maintain the level of precision defined.:
<?php
// called as NumberHelper
echo $this->Number->precision(456.91873645, 2);
// Outputs
456.92
// called as CakeNumber
App::uses('CakeNumber', 'Utility');
echo CakeNumber::precision(456.91873645, 2);
Parameters: |
|
---|
Like precision(), this method formats a number according to the supplied precision (where numbers are rounded to meet the given precision). This method also expresses the number as a percentage and prepends the output with a percent sign.:
<?php
// called as NumberHelper
echo $this->Number->toPercentage(45.691873645);
// Outputs
45.69%
// called as CakeNumber
App::uses('CakeNumber', 'Utility');
echo CakeNumber::toPercentage(45.691873645);
Parameters: |
|
---|
This method formats data sizes in human readable forms. It provides a shortcut way to convert bytes to KB, MB, GB, and TB. The size is displayed with a two-digit precision level, according to the size of data supplied (i.e. higher sizes are expressed in larger terms):
<?php
// called as NumberHelper
echo $this->Number->toReadableSize(0); // 0 Bytes
echo $this->Number->toReadableSize(1024); // 1 KB
echo $this->Number->toReadableSize(1321205.76); // 1.26 MB
echo $this->Number->toReadableSize(5368709120); // 5.00 GB
// called as CakeNumber
App::uses('CakeNumber', 'Utility');
echo CakeNumber::toReadableSize(0); // 0 Bytes
echo CakeNumber::toReadableSize(1024); // 1 KB
echo CakeNumber::toReadableSize(1321205.76); // 1.26 MB
echo CakeNumber::toReadableSize(5368709120); // 5.00 GB
This method gives you much more control over the formatting of numbers for use in your views (and is used as the main method by most of the other NumberHelper methods). Using this method might looks like:
<?php
// called as NumberHelper
$this->Number->format($number, $options);
// called as CakeNumber
CakeNumber::format($number, $options);
The $number parameter is the number that you are planning on formatting for output. With no $options supplied, the number 1236.334 would output as 1,236. Note that the default precision is zero decimal places.
The $options parameter is where the real magic for this method resides.
Example:
<?php
// called as NumberHelper
echo $this->Number->format('123456.7890', array(
'places' => 2,
'before' => '¥ ',
'escape' => false,
'decimals' => '.',
'thousands' => ','
));
// output '¥ 123,456.79'
// called as CakeNumber
App::uses('CakeNumber', 'Utility');
echo CakeNumber::format('123456.7890', array(
'places' => 2,
'before' => '¥ ',
'escape' => false,
'decimals' => '.',
'thousands' => ','
));
// output '¥ 123,456.79'