Number Helper

The Number Helper file contains functions that help you work withnumeric data in a locale-aware manner.

Loading this Helper

This helper is loaded using the following code:

  1. helper('number');

When Things Go Wrong

If PHP’s internationalization and localization logic cannot handlea value provided, for the given locale and options, then aBadFunctionCallException() will be thrown.

Available Functions

The following functions are available:

  • numberto_size($num[, $precision = 1[, $locale = null]_)

Parameters:

  • $num (mixed) – Number of bytes
  • $precision (int) – Floating point precisionReturns:Formatted data size string, or false if the provided value is not numericReturn type:string

Formats numbers as bytes, based on size, and adds the appropriatesuffix. Examples:

  1. echo number_to_size(456); // Returns 456 Bytes
  2. echo number_to_size(4567); // Returns 4.5 KB
  3. echo number_to_size(45678); // Returns 44.6 KB
  4. echo number_to_size(456789); // Returns 447.8 KB
  5. echo number_to_size(3456789); // Returns 3.3 MB
  6. echo number_to_size(12345678912345); // Returns 1.8 GB
  7. echo number_to_size(123456789123456789); // Returns 11,228.3 TB

An optional second parameter allows you to set the precision of the result:

  1. echo number_to_size(45678, 2); // Returns 44.61 KB

An optional third parameter allows you to specify the locale that shouldbe used when generating the number, and can affect the formatting. If nolocale is specified, the Request will be analyzed and an appropriatelocale taken from the headers, or the app-default:

  1. // Generates 11.2 TB
  2. echo number_to_size(12345678912345, 1, 'en_US');
  3. // Generates 11,2 TB
  4. echo number_to_size(12345678912345, 1, 'fr_FR');

Note

The text generated by this function is found in the followinglanguage file: language//Number.php

  • numberto_amount($num[, $precision = 1[, $locale = null]_)

Parameters:

  • $num (mixed) – Number to format
  • $precision (int) – Floating point precision
  • $locale (string) – The locale to use for formattingReturns:A human-readable version of the string, or false if the provided value is not numericReturn type:string

Converts a number into a human-readable version, like 123.4 trillionfor numbers up to the quadrillions. Examples:

  1. echo number_to_amount(123456); // Returns 123 thousand
  2. echo number_to_amount(123456789); // Returns 123 million
  3. echo number_to_amount(1234567890123, 2); // Returns 1.23 trillion
  4. echo number_to_amount('123,456,789,012', 2); // Returns 123.46 billion

An optional second parameter allows you to set the precision of the result:

  1. echo number_to_amount(45678, 2); // Returns 45.68 thousand

An optional third parameter allows the locale to be specified:

  1. echo number_to_amount('123,456,789,012', 2, 'de_DE'); // Returns 123,46 billion
  • numberto_currency($num, $currency[, $locale = null_])

Parameters:

  • $num (mixed) – Number to format
  • $currency (string) – The currency type, i.e. USD, EUR, etc
  • $locale (string) – The locale to use for formattingReturns:The number as the appropriate currency for the localeReturn type:string

Converts a number in common currency formats, like USD, EUR, GBP, etc:

  1. echo number_to_currency(1234.56, 'USD'); // Returns $1,234.56
  2. echo number_to_currency(1234.56, 'EUR'); // Returns £1,234.56
  3. echo number_to_currency(1234.56, 'GBP'); // Returns £1,234.56
  4. echo number_to_currency(1234.56, 'YEN'); // Returns YEN1,234.56
  • numberto_roman($num_)

Parameters:

  • $num (string) – The number want to convertReturns:The roman number converted from given parameterReturn type:string|null

Converts a number into roman:

  1. echo number_to_roman(23); // Returns XXIII
  2. echo number_to_roman(324); // Returns CCCXXIV
  3. echo number_to_roman(2534); // Returns MMDXXXIV

This function only handles numbers in the range 1 through 3999.It will return null for any value outside that range .