|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * CodeIgniter |
| 4 | + * |
| 5 | + * An open source application development framework for PHP |
| 6 | + * |
| 7 | + * This content is released under the MIT License (MIT) |
| 8 | + * |
| 9 | + * Copyright (c) 2014 - 2016, British Columbia Institute of Technology |
| 10 | + * |
| 11 | + * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 12 | + * of this software and associated documentation files (the "Software"), to deal |
| 13 | + * in the Software without restriction, including without limitation the rights |
| 14 | + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 15 | + * copies of the Software, and to permit persons to whom the Software is |
| 16 | + * furnished to do so, subject to the following conditions: |
| 17 | + * |
| 18 | + * The above copyright notice and this permission notice shall be included in |
| 19 | + * all copies or substantial portions of the Software. |
| 20 | + * |
| 21 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 22 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 23 | + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 24 | + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 25 | + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 26 | + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 27 | + * THE SOFTWARE. |
| 28 | + * |
| 29 | + * @package CodeIgniter |
| 30 | + * @author EllisLab Dev Team |
| 31 | + * @copyright Copyright (c) 2008 - 2014, EllisLab, Inc. (https://ellislab.com/) |
| 32 | + * @copyright Copyright (c) 2014 - 2016, British Columbia Institute of Technology (http://bcit.ca/) |
| 33 | + * @license http://opensource.org/licenses/MIT MIT License |
| 34 | + * @link https://codeigniter.com |
| 35 | + * @since Version 1.0.0 |
| 36 | + * @filesource |
| 37 | + */ |
| 38 | + |
| 39 | + |
| 40 | +if ( ! function_exists('number_to_size')) |
| 41 | +{ |
| 42 | + /** |
| 43 | + * Formats a numbers as bytes, based on size, and adds the appropriate suffix |
| 44 | + * |
| 45 | + * @param mixed will be cast as int |
| 46 | + * @param int |
| 47 | + * @return string |
| 48 | + */ |
| 49 | + function number_to_size($num, int $precision = 1, string $locale=null) |
| 50 | + { |
| 51 | + // Strip any formatting |
| 52 | + $num = 0 + str_replace(',','',$num); |
| 53 | + |
| 54 | + // Can't work with non-numbers... |
| 55 | + if (! is_numeric($num)) |
| 56 | + { |
| 57 | + return false; |
| 58 | + } |
| 59 | + |
| 60 | + if ($num >= 1000000000000) |
| 61 | + { |
| 62 | + $num = round($num / 1099511627776, $precision); |
| 63 | + $unit = lang('Number.terabyteAbbr'); |
| 64 | + } |
| 65 | + elseif ($num >= 1000000000) |
| 66 | + { |
| 67 | + $num = round($num / 1073741824, $precision); |
| 68 | + $unit = lang('Number.gigabyteAbbr'); |
| 69 | + } |
| 70 | + elseif ($num >= 1000000) |
| 71 | + { |
| 72 | + $num = round($num / 1048576, $precision); |
| 73 | + $unit = lang('Number.megabyteAbbr'); |
| 74 | + } |
| 75 | + elseif ($num >= 1000) |
| 76 | + { |
| 77 | + $num = round($num / 1024, $precision); |
| 78 | + $unit = lang('Number.kilobyteAbbr'); |
| 79 | + } |
| 80 | + else |
| 81 | + { |
| 82 | + $unit = lang('Number.bytes'); |
| 83 | + } |
| 84 | + |
| 85 | + return format_number($num, $precision, $locale, ['after' => ' '.$unit]); |
| 86 | + } |
| 87 | +} |
| 88 | + |
| 89 | +//-------------------------------------------------------------------- |
| 90 | + |
| 91 | +if (! function_exists('number_to_amount')) |
| 92 | +{ |
| 93 | + /** |
| 94 | + * Converts numbers to a more readable representation |
| 95 | + * when dealing with very large numbers (in the thousands or above), |
| 96 | + * up to the quadrillions, because you won't often deal with numbers |
| 97 | + * larger than that. |
| 98 | + * |
| 99 | + * It uses the "short form" numbering system as this is most commonly |
| 100 | + * used within most English-speaking countries today. |
| 101 | + * |
| 102 | + * @see https://simple.wikipedia.org/wiki/Names_for_large_numbers |
| 103 | + * |
| 104 | + * @param $num |
| 105 | + * @param int $precision |
| 106 | + * @param string|null $locale |
| 107 | + * |
| 108 | + * @return bool|string |
| 109 | + */ |
| 110 | + function number_to_amount($num, int $precision = 0, string $locale = null) |
| 111 | + { |
| 112 | + // Strip any formatting |
| 113 | + $num = 0 + str_replace(',','',$num); |
| 114 | + |
| 115 | + // Can't work with non-numbers... |
| 116 | + if (! is_numeric($num)) |
| 117 | + { |
| 118 | + return false; |
| 119 | + } |
| 120 | + |
| 121 | + $suffix = ''; |
| 122 | + |
| 123 | + if ($num > 1000000000000000) |
| 124 | + { |
| 125 | + $suffix = lang('Number.quadrillion'); |
| 126 | + $num = round(($num / 1000000000000000), $precision); |
| 127 | + } |
| 128 | + elseif ($num > 1000000000000) |
| 129 | + { |
| 130 | + $suffix = lang('Number.trillion'); |
| 131 | + $num = round(($num / 1000000000000), $precision); |
| 132 | + } |
| 133 | + else if ($num > 1000000000) |
| 134 | + { |
| 135 | + $suffix = lang('Number.billion'); |
| 136 | + $num = round(($num/1000000000), $precision); |
| 137 | + } |
| 138 | + else if ($num > 1000000) |
| 139 | + { |
| 140 | + $suffix = lang('Number.million'); |
| 141 | + $num = round(($num/1000000), $precision); |
| 142 | + } |
| 143 | + else if ($num > 1000) |
| 144 | + { |
| 145 | + $suffix = lang('Number.thousand'); |
| 146 | + $num = round(($num/1000), $precision); |
| 147 | + } |
| 148 | + |
| 149 | + return format_number($num, $precision, $locale, ['after' => $suffix]); |
| 150 | + } |
| 151 | +} |
| 152 | + |
| 153 | +//-------------------------------------------------------------------- |
| 154 | + |
| 155 | +if (! function_exists('number_to_currency')) |
| 156 | +{ |
| 157 | + function number_to_currency($num, string $currency, string $locale = null) |
| 158 | + { |
| 159 | + return format_number($num, 1, $locale, [ |
| 160 | + 'type' => NumberFormatter::CURRENCY, |
| 161 | + 'currency' => $currency |
| 162 | + ]); |
| 163 | + } |
| 164 | +} |
| 165 | + |
| 166 | +//-------------------------------------------------------------------- |
| 167 | + |
| 168 | +if (! function_exists('format_number')) |
| 169 | +{ |
| 170 | + /** |
| 171 | + * A general purpose, locale-aware, number_format method. |
| 172 | + * Used by all of the functions of the number_helper. |
| 173 | + * |
| 174 | + * @param $num |
| 175 | + * @param int $precision |
| 176 | + * @param string|null $locale |
| 177 | + * @param array $options |
| 178 | + * |
| 179 | + * @return string |
| 180 | + */ |
| 181 | + function format_number($num, int $precision = 1, string $locale = null, array $options=[]) |
| 182 | + { |
| 183 | + // Locale is either passed in here, negotiated with client, or grabbed from our config file. |
| 184 | + $locale = $locale ?? \CodeIgniter\Config\Services::request()->getLocale(); |
| 185 | + |
| 186 | + // Type can be any of the NumberFormatter options, but provide a default. |
| 187 | + $type = isset($options['type']) |
| 188 | + ? (int)$options['type'] : |
| 189 | + NumberFormatter::DECIMAL; |
| 190 | + |
| 191 | + // In order to specify a precision, we'll have to modify |
| 192 | + // the pattern used by NumberFormatter. |
| 193 | + $pattern = '#,##0.'. str_repeat('#', $precision); |
| 194 | + |
| 195 | + $formatter = new NumberFormatter($locale, $type); |
| 196 | + |
| 197 | + // Try to format it per the locale |
| 198 | + if ($type == NumberFormatter::CURRENCY) |
| 199 | + { |
| 200 | + $output = $formatter->formatCurrency($num, $options['currency']); |
| 201 | + } |
| 202 | + else |
| 203 | + { |
| 204 | + $formatter->setPattern($pattern); |
| 205 | + $output = $formatter->format($num); |
| 206 | + } |
| 207 | + |
| 208 | + // This might lead a trailing period if $precision == 0 |
| 209 | + $output = trim($output, '. '); |
| 210 | + |
| 211 | + if (intl_is_failure($formatter->getErrorCode())) |
| 212 | + { |
| 213 | + throw new BadFunctionCallException($formatter->getErrorMessage()); |
| 214 | + } |
| 215 | + |
| 216 | + // Add on any before/after text. |
| 217 | + if (isset($options['before']) && is_string($options['before'])) |
| 218 | + { |
| 219 | + $output = $options['before'].$output; |
| 220 | + } |
| 221 | + |
| 222 | + if (isset($options['after']) && is_string($options['after'])) |
| 223 | + { |
| 224 | + $output .= $options['after']; |
| 225 | + } |
| 226 | + |
| 227 | + return $output; |
| 228 | + } |
| 229 | +} |
| 230 | + |
| 231 | +//-------------------------------------------------------------------- |
0 commit comments