|
| 1 | +############# |
| 2 | +Number Helper |
| 3 | +############# |
| 4 | + |
| 5 | +The Number Helper file contains functions that help you work with |
| 6 | +numeric data in a locale-aware manner. |
| 7 | + |
| 8 | +.. contents:: |
| 9 | +:local: |
| 10 | + |
| 11 | +.. raw:: html |
| 12 | + |
| 13 | + <div class="custom-index container"></div> |
| 14 | + |
| 15 | +Loading this Helper |
| 16 | +=================== |
| 17 | + |
| 18 | +This helper is loaded using the following code:: |
| 19 | + |
| 20 | + helper('number'); |
| 21 | + |
| 22 | +Available Functions |
| 23 | +=================== |
| 24 | + |
| 25 | +The following functions are available: |
| 26 | + |
| 27 | + |
| 28 | +.. php:function:: number_to_size($num[, $precision = 1[, $locale = null]) |
| 29 | +
|
| 30 | + :param mixed $num: Number of bytes |
| 31 | + :param int $precision: Floating point precision |
| 32 | + :returns: Formatted data size string |
| 33 | + :rtype: string |
| 34 | + |
| 35 | + Formats numbers as bytes, based on size, and adds the appropriate |
| 36 | + suffix. Examples:: |
| 37 | + |
| 38 | + echo number_to_size(456); // Returns 456 Bytes |
| 39 | + echo number_to_size(4567); // Returns 4.5 KB |
| 40 | + echo number_to_size(45678); // Returns 44.6 KB |
| 41 | + echo number_to_size(456789); // Returns 447.8 KB |
| 42 | + echo number_to_size(3456789); // Returns 3.3 MB |
| 43 | + echo number_to_size(12345678912345); // Returns 1.8 GB |
| 44 | + echo number_to_size(123456789123456789); // Returns 11,228.3 TB |
| 45 | + |
| 46 | + An optional second parameter allows you to set the precision of the |
| 47 | + result:: |
| 48 | + |
| 49 | + echo number_to_size(45678, 2); // Returns 44.61 KB |
| 50 | + |
| 51 | + An optional third parameter allows you to specify the locale that should |
| 52 | + be used when generating the number, and can affect the formatting. If no |
| 53 | + locale is specified, the Request will be analyzed and an appropriate |
| 54 | + locale taken from the headers, or the app-default:: |
| 55 | + |
| 56 | + // Generates 11.2 TB |
| 57 | + echo number_to_size(12345678912345, 1, 'en_US'); |
| 58 | + // Generates 11,2 TB |
| 59 | + echo number_to_size(12345678912345, 1, 'fr_FR'); |
| 60 | + |
| 61 | + |
| 62 | + .. note:: The text generated by this function is found in the following |
| 63 | + language file: *language/<your_lang>/Number.php* |
| 64 | + |
| 65 | +.. php:function:: number_to_amount($num[, $precision = 1[, $locale = null]) |
| 66 | +
|
| 67 | + :param mixed $num: Number to format |
| 68 | + :param int $precision: Floating point precision |
| 69 | + :param string $locale: The locale to use for formatting |
| 70 | + :returns: A human-readable version of the string |
| 71 | + :rtype: string |
| 72 | + |
| 73 | + Converts a number into a human-readable version, like **123.4 trillion** |
| 74 | + for numbers up to the quadrillions. Examples:: |
| 75 | + |
| 76 | + echo number_to_amount(123456); // Returns 123 thousand |
| 77 | + echo number_to_amount(123456789); // Returns 123 million |
| 78 | + echo number_to_amount(1234567890123, 2); // Returns 1.23 trillion |
| 79 | + echo number_to_amount('123,456,789,012', 2); // Returns 123.46 billion |
| 80 | + |
| 81 | + An optional second parameter allows you to set the precision of the |
| 82 | + result:: |
| 83 | + |
| 84 | + echo number_to_amount(45678, 2); // Returns 45.68 thousand |
| 85 | + |
| 86 | + An optional third parameter allows the locale to be specified:: |
| 87 | + |
| 88 | + echo number_to_amount('123,456,789,012', 2, 'de_DE'); // Returns 123,46 billion |
| 89 | + |
| 90 | +.. php:function:: number_to_currency($num, $currency[, $locale = null]) |
| 91 | +
|
| 92 | + :param mixed $num: Number to format |
| 93 | + :param string $currency: The currency type, i.e. USD, EUR, etc |
| 94 | + :param string $locale: The locale to use for formatting |
| 95 | + :returns: The number as the appropriate currency for the locale |
| 96 | + :rtype: string |
| 97 | + |
| 98 | + Converts a number in common currency formats, like USD, EUR, GBP, etc:: |
| 99 | + |
| 100 | + echo number_to_currency(1234.56, 'USD'); // Returns $1,234.56 |
| 101 | + echo number_to_currency(1234.56, 'EUR'); // Returns £1,234.56 |
| 102 | + echo number_to_currency(1234.56, 'GBP'); // Returns £1,234.56 |
| 103 | + echo number_to_currency(1234.56, 'YEN'); // Returns YEN1,234.56 |
0 commit comments