Number

  • class Cake\I18n\Number

If you need NumberHelper functionalities outside of a View,use the Number class:

  1. namespace App\Controller;
  2.  
  3. use Cake\I18n\Number;
  4.  
  5. class UsersController extends AppController
  6. {
  7. public function initialize(): void
  8. {
  9. parent::initialize();
  10. $this->loadComponent('Auth');
  11. }
  12.  
  13. public function afterLogin()
  14. {
  15. $storageUsed = $this->Auth->user('storage_used');
  16. if ($storageUsed > 5000000) {
  17. // Notify users of quota
  18. $this->Flash->success(__('You are using {0} storage', Number::toReadableSize($storageUsed)));
  19. }
  20. }
  21. }

All of these functions return the formatted number; they do notautomatically echo the output into the view.

Formatting Currency Values

  • 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:

  1. // Called as NumberHelper
  2. echo $this->Number->currency($value, $currency);
  3.  
  4. // Called as Number
  5. 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:

$currency1234.56, formatted by currency type
EUR€1.234,56
GBP£1,234.56
USD$1,234.56

The third parameter is an array of options for further defining theoutput. The following options are available:

OptionDescription
beforeText to display before the rendered number.
afterText to display after the rendered number.
zeroThe text to use for zero values; can be a stringor a number. ie. 0, ‘Free!’.
placesNumber of decimal places to use, ie. 2
precisionMaximal number of decimal places to use, ie. 2
localeThe locale name to use for formatting number,ie. “fr_FR”.
fractionSymbolString to use for fraction numbers, ie. ‘ cents’.
fractionPositionEither ‘before’ or ‘after’ to place the fractionsymbol.
patternAn ICU number pattern to use for formatting thenumber ie. #,###.00
useIntlCodeSet to true to replace the currency symbolwith the international currency code.

If $currency value is null, the default currency will be retrieved fromCake\I18n\Number::defaultCurrency(). To format currencies in anaccounting format you should set the currency format:

  1. Number::setDefaultCurrencyFormat(Number::FORMAT_CURRENCY_ACCOUNTING);

New in version 3.9.0: Formatting currency as accounting values was added.

Setting the Default Currency

  • Cake\I18n\Number::defaultCurrency($currency)

Setter/getter for the default currency. This removes the need to always pass thecurrency to Cake\I18n\Number::currency() and change allcurrency outputs by setting other default. If $currency is set to false,it will clear the currently stored value. By default, it will retrieve theintl.default_locale if set and ‘en_US’ if not.

Formatting Floating Point Numbers

  • 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.

  1. // Called as NumberHelper
  2. echo $this->Number->precision(456.91873645, 2);
  3.  
  4. // Outputs
  5. 456.92
  6.  
  7. // Called as Number
  8. echo Number::precision(456.91873645, 2);

Formatting Percentages

  • Cake\I18n\Number::toPercentage(mixed $value, int $precision = 2, array $options = [])
OptionDescription
multiplyBoolean to indicate whether the value has to bemultiplied by 100. Useful for decimal percentages.

Like Cake\I18n\Number::precision(), this method formats a numberaccording to the supplied precision (where numbers are rounded to meet thegiven precision). This method also expresses the number as a percentageand appends the output with a percent sign.

  1. // Called as NumberHelper. Output: 45.69%
  2. echo $this->Number->toPercentage(45.691873645);
  3.  
  4. // Called as Number. Output: 45.69%
  5. echo Number::toPercentage(45.691873645);
  6.  
  7. // Called with multiply. Output: 45.7%
  8. echo Number::toPercentage(0.45691, 1, [
  9. 'multiply' => true
  10. ]);

Interacting with Human Readable Values

  • 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):

  1. // Called as NumberHelper
  2. echo $this->Number->toReadableSize(0); // 0 Byte
  3. echo $this->Number->toReadableSize(1024); // 1 KB
  4. echo $this->Number->toReadableSize(1321205.76); // 1.26 MB
  5. echo $this->Number->toReadableSize(5368709120); // 5 GB
  6.  
  7. // Called as Number
  8. echo Number::toReadableSize(0); // 0 Byte
  9. echo Number::toReadableSize(1024); // 1 KB
  10. echo Number::toReadableSize(1321205.76); // 1.26 MB
  11. 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:

  1. // Called as NumberHelper
  2. $this->Number->format($value, $options);
  3.  
  4. // Called as Number
  5. Number::format($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 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:
OptionDescription
placesNumber of decimal places to use, ie. 2
precisionMaximum number of decimal places to use, ie. 2
patternAn ICU number pattern to use for formatting thenumber ie. #,###.00
localeThe locale name to use for formatting number,ie. “fr_FR”.
beforeText to display before the rendered number.
afterText to display after the rendered number.

Example:

  1. // Called as NumberHelper
  2. echo $this->Number->format('123456.7890', [
  3. 'places' => 2,
  4. 'before' => '¥ ',
  5. 'after' => ' !'
  6. ]);
  7. // Output '¥ 123,456.79 !'
  8.  
  9. echo $this->Number->format('123456.7890', [
  10. 'locale' => 'fr_FR'
  11. ]);
  12. // Output '123 456,79 !'
  13.  
  14. // Called as Number
  15. echo Number::format('123456.7890', [
  16. 'places' => 2,
  17. 'before' => '¥ ',
  18. 'after' => ' !'
  19. ]);
  20. // Output '¥ 123,456.79 !'
  21.  
  22. echo Number::format('123456.7890', [
  23. 'locale' => 'fr_FR'
  24. ]);
  25. // Output '123 456,79 !'
  • Cake\I18n\Number::ordinal(mixed $value, array $options = [])

This method will output an ordinal number.

Examples:

  1. echo Number::ordinal(1);
  2. // Output '1st'
  3.  
  4. echo Number::ordinal(2);
  5. // Output '2nd'
  6.  
  7. echo Number::ordinal(2, [
  8. 'locale' => 'fr_FR'
  9. ]);
  10. // Output '2e'
  11.  
  12. echo Number::ordinal(410);
  13. // Output '410th'

Format Differences

  • Cake\I18n\Number::formatDelta(mixed $value, array $options = [])

This method displays differences in value as a signed number:

  1. // Called as NumberHelper
  2. $this->Number->formatDelta($value, $options);
  3.  
  4. // Called as Number
  5. 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 Number::format() itself:

OptionDescription
placesNumber of decimal places to use, ie. 2
precisionMaximum number of decimal places to use, ie. 2
localeThe locale name to use for formatting number,ie. “fr_FR”.
beforeText to display before the rendered number.
afterText to display after the rendered number.

Example:

  1. // Called as NumberHelper
  2. echo $this->Number->formatDelta('123456.7890', [
  3. 'places' => 2,
  4. 'before' => '[',
  5. 'after' => ']'
  6. ]);
  7. // Output '[+123,456.79]'
  8.  
  9. // Called as Number
  10. echo Number::formatDelta('123456.7890', [
  11. 'places' => 2,
  12. 'before' => '[',
  13. 'after' => ']'
  14. ]);
  15. // Output '[+123,456.79]'

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.

Example:

  1. Number::setConfig('en_IN', \NumberFormatter::CURRENCY, [
  2. 'pattern' => '#,##,##0'
  3. ]);