-
Notifications
You must be signed in to change notification settings - Fork 162
/
Number.php
384 lines (328 loc) · 10.7 KB
/
Number.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
<?php
namespace Illuminate\Support;
use Illuminate\Support\Traits\Macroable;
use NumberFormatter;
use RuntimeException;
class Number
{
use Macroable;
/**
* The current default locale.
*
* @var string
*/
protected static $locale = 'en';
/**
* The current default currency.
*
* @var string
*/
protected static $currency = 'USD';
/**
* Format the given number according to the current locale.
*
* @param int|float $number
* @param int|null $precision
* @param int|null $maxPrecision
* @param string|null $locale
* @return string|false
*/
public static function format(int|float $number, ?int $precision = null, ?int $maxPrecision = null, ?string $locale = null)
{
static::ensureIntlExtensionIsInstalled();
$formatter = new NumberFormatter($locale ?? static::$locale, NumberFormatter::DECIMAL);
if (! is_null($maxPrecision)) {
$formatter->setAttribute(NumberFormatter::MAX_FRACTION_DIGITS, $maxPrecision);
} elseif (! is_null($precision)) {
$formatter->setAttribute(NumberFormatter::FRACTION_DIGITS, $precision);
}
return $formatter->format($number);
}
/**
* Spell out the given number in the given locale.
*
* @param int|float $number
* @param string|null $locale
* @param int|null $after
* @param int|null $until
* @return string
*/
public static function spell(int|float $number, ?string $locale = null, ?int $after = null, ?int $until = null)
{
static::ensureIntlExtensionIsInstalled();
if (! is_null($after) && $number <= $after) {
return static::format($number, locale: $locale);
}
if (! is_null($until) && $number >= $until) {
return static::format($number, locale: $locale);
}
$formatter = new NumberFormatter($locale ?? static::$locale, NumberFormatter::SPELLOUT);
return $formatter->format($number);
}
/**
* Convert the given number to ordinal form.
*
* @param int|float $number
* @param string|null $locale
* @return string
*/
public static function ordinal(int|float $number, ?string $locale = null)
{
static::ensureIntlExtensionIsInstalled();
$formatter = new NumberFormatter($locale ?? static::$locale, NumberFormatter::ORDINAL);
return $formatter->format($number);
}
/**
* Spell out the given number in the given locale in ordinal form.
*
* @param int|float $number
* @param string|null $locale
* @return string
*/
public static function spellOrdinal(int|float $number, ?string $locale = null)
{
static::ensureIntlExtensionIsInstalled();
$formatter = new NumberFormatter($locale ?? static::$locale, NumberFormatter::SPELLOUT);
$formatter->setTextAttribute(NumberFormatter::DEFAULT_RULESET, '%spellout-ordinal');
return $formatter->format($number);
}
/**
* Convert the given number to its percentage equivalent.
*
* @param int|float $number
* @param int $precision
* @param int|null $maxPrecision
* @param string|null $locale
* @return string|false
*/
public static function percentage(int|float $number, int $precision = 0, ?int $maxPrecision = null, ?string $locale = null)
{
static::ensureIntlExtensionIsInstalled();
$formatter = new NumberFormatter($locale ?? static::$locale, NumberFormatter::PERCENT);
if (! is_null($maxPrecision)) {
$formatter->setAttribute(NumberFormatter::MAX_FRACTION_DIGITS, $maxPrecision);
} else {
$formatter->setAttribute(NumberFormatter::FRACTION_DIGITS, $precision);
}
return $formatter->format($number / 100);
}
/**
* Convert the given number to its currency equivalent.
*
* @param int|float $number
* @param string $in
* @param string|null $locale
* @return string|false
*/
public static function currency(int|float $number, string $in = '', ?string $locale = null)
{
static::ensureIntlExtensionIsInstalled();
$formatter = new NumberFormatter($locale ?? static::$locale, NumberFormatter::CURRENCY);
return $formatter->formatCurrency($number, ! empty($in) ? $in : static::$currency);
}
/**
* Convert the given number to its file size equivalent.
*
* @param int|float $bytes
* @param int $precision
* @param int|null $maxPrecision
* @return string
*/
public static function fileSize(int|float $bytes, int $precision = 0, ?int $maxPrecision = null)
{
$units = ['B', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'];
for ($i = 0; ($bytes / 1024) > 0.9 && ($i < count($units) - 1); $i++) {
$bytes /= 1024;
}
return sprintf('%s %s', static::format($bytes, $precision, $maxPrecision), $units[$i]);
}
/**
* Convert the number to its human-readable equivalent.
*
* @param int|float $number
* @param int $precision
* @param int|null $maxPrecision
* @return bool|string
*/
public static function abbreviate(int|float $number, int $precision = 0, ?int $maxPrecision = null)
{
return static::forHumans($number, $precision, $maxPrecision, abbreviate: true);
}
/**
* Convert the number to its human-readable equivalent.
*
* @param int|float $number
* @param int $precision
* @param int|null $maxPrecision
* @param bool $abbreviate
* @return false|string
*/
public static function forHumans(int|float $number, int $precision = 0, ?int $maxPrecision = null, bool $abbreviate = false)
{
return static::summarize($number, $precision, $maxPrecision, $abbreviate ? [
3 => 'K',
6 => 'M',
9 => 'B',
12 => 'T',
15 => 'Q',
] : [
3 => ' thousand',
6 => ' million',
9 => ' billion',
12 => ' trillion',
15 => ' quadrillion',
]);
}
/**
* Convert the number to its human-readable equivalent.
*
* @param int|float $number
* @param int $precision
* @param int|null $maxPrecision
* @param array $units
* @return string|false
*/
protected static function summarize(int|float $number, int $precision = 0, ?int $maxPrecision = null, array $units = [])
{
if (empty($units)) {
$units = [
3 => 'K',
6 => 'M',
9 => 'B',
12 => 'T',
15 => 'Q',
];
}
switch (true) {
case floatval($number) === 0.0:
return $precision > 0 ? static::format(0, $precision, $maxPrecision) : '0';
case $number < 0:
return sprintf('-%s', static::summarize(abs($number), $precision, $maxPrecision, $units));
case $number >= 1e15:
return sprintf('%s'.end($units), static::summarize($number / 1e15, $precision, $maxPrecision, $units));
}
$numberExponent = floor(log10($number));
$displayExponent = $numberExponent - ($numberExponent % 3);
$number /= pow(10, $displayExponent);
return trim(sprintf('%s%s', static::format($number, $precision, $maxPrecision), $units[$displayExponent] ?? ''));
}
/**
* Clamp the given number between the given minimum and maximum.
*
* @param int|float $number
* @param int|float $min
* @param int|float $max
* @return int|float
*/
public static function clamp(int|float $number, int|float $min, int|float $max)
{
return min(max($number, $min), $max);
}
/**
* Split the given number into pairs of min/max values.
*
* @param int|float $to
* @param int|float $by
* @param int|float $start
* @param int|float $offset
* @return array
*/
public static function pairs(int|float $to, int|float $by, int|float $start = 0, int|float $offset = 1)
{
$output = [];
for ($lower = $start; $lower < $to; $lower += $by) {
$upper = $lower + $by - $offset;
if ($upper > $to) {
$upper = $to;
}
$output[] = [$lower, $upper];
}
return $output;
}
/**
* Remove any trailing zero digits after the decimal point of the given number.
*
* @param int|float $number
* @return int|float
*/
public static function trim(int|float $number)
{
return json_decode(json_encode($number));
}
/**
* Execute the given callback using the given locale.
*
* @param string $locale
* @param callable $callback
* @return mixed
*/
public static function withLocale(string $locale, callable $callback)
{
$previousLocale = static::$locale;
static::useLocale($locale);
return tap($callback(), fn () => static::useLocale($previousLocale));
}
/**
* Execute the given callback using the given currency.
*
* @param string $currency
* @param callable $callback
* @return mixed
*/
public static function withCurrency(string $currency, callable $callback)
{
$previousCurrency = static::$currency;
static::useCurrency($currency);
return tap($callback(), fn () => static::useCurrency($previousCurrency));
}
/**
* Set the default locale.
*
* @param string $locale
* @return void
*/
public static function useLocale(string $locale)
{
static::$locale = $locale;
}
/**
* Set the default currency.
*
* @param string $currency
* @return void
*/
public static function useCurrency(string $currency)
{
static::$currency = $currency;
}
/**
* Get the default locale.
*
* @return string
*/
public static function defaultLocale()
{
return static::$locale;
}
/**
* Get the default currency.
*
* @return string
*/
public static function defaultCurrency()
{
return static::$currency;
}
/**
* Ensure the "intl" PHP extension is installed.
*
* @return void
*/
protected static function ensureIntlExtensionIsInstalled()
{
if (! extension_loaded('intl')) {
$method = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 2)[1]['function'];
throw new RuntimeException('The "intl" PHP extension is required to use the ['.$method.'] method.');
}
}
}