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:
- 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:
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:
- echo number_to_size(456); // Returns 456 Bytes
- echo number_to_size(4567); // Returns 4.5 KB
- echo number_to_size(45678); // Returns 44.6 KB
- echo number_to_size(456789); // Returns 447.8 KB
- echo number_to_size(3456789); // Returns 3.3 MB
- echo number_to_size(12345678912345); // Returns 1.8 GB
- echo number_to_size(123456789123456789); // Returns 11,228.3 TB
An optional second parameter allows you to set the precision of the result:
- 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:
- // Generates 11.2 TB
- echo number_to_size(12345678912345, 1, 'en_US');
- // Generates 11,2 TB
- echo number_to_size(12345678912345, 1, 'fr_FR');
Note
The text generated by this function is found in the followinglanguage file: language/
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:
- echo number_to_amount(123456); // Returns 123 thousand
- echo number_to_amount(123456789); // Returns 123 million
- echo number_to_amount(1234567890123, 2); // Returns 1.23 trillion
- 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:
- echo number_to_amount(45678, 2); // Returns 45.68 thousand
An optional third parameter allows the locale to be specified:
- echo number_to_amount('123,456,789,012', 2, 'de_DE'); // Returns 123,46 billion
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:
- echo number_to_currency(1234.56, 'USD'); // Returns $1,234.56
- echo number_to_currency(1234.56, 'EUR'); // Returns £1,234.56
- echo number_to_currency(1234.56, 'GBP'); // Returns £1,234.56
- echo number_to_currency(1234.56, 'YEN'); // Returns YEN1,234.56
Parameters:
- $num (string) – The number want to convertReturns:The roman number converted from given parameterReturn type:string|null
Converts a number into roman:
- echo number_to_roman(23); // Returns XXIII
- echo number_to_roman(324); // Returns CCCXXIV
- 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 .