-
Notifications
You must be signed in to change notification settings - Fork 0
/
Number.hh
357 lines (315 loc) · 10.1 KB
/
Number.hh
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
<?hh // strict
/**
* @copyright 2010-2015, The Titon Project
* @license http://opensource.org/licenses/bsd-license.php
* @link http://titon.io
*/
namespace Titon\Utility;
/**
* The Number utility allows for the twiddling and calculation of numbers and floats.
* Provides helper methods to ease in the evaluation of numbers within context.
*
* @package Titon\Utility
*/
class Number {
/**
* Bases.
*/
const int BINARY = 2;
const int OCTAL = 8;
const int DECIMAL = 10;
const int HEX = 16;
/**
* Convert a readable string notated form of bytes (1KB) to the numerical equivalent (1024).
* Supports all the different format variations: k, kb, ki, kib, etc.
*
* @param string $number
* @return int
*/
public static function bytesFrom(string $number): int {
if (!$number) {
return 0;
} else if (is_numeric($number)) {
return (int) $number;
}
$number = trim((string) $number);
$sizes = [
'k|kb|ki|kib' => 10,
'm|mb|mi|mib' => 20,
'g|gb|gi|gib' => 30,
't|tb|ti|tib' => 40,
'p|pb|pi|pib' => 50,
'e|eb|ei|eib' => 60,
'z|zb|zi|zib' => 70,
'y|yb|yi|yib' => 80,
'b' => 0
];
foreach ($sizes as $format => $pow) {
$matches = Vector {}; // Type checker needs it defined
if (preg_match('/^([0-9\.]+)(' . $format . ')$/i', $number, $matches)) {
return (int) (((float) $matches[1]) * pow(2, $pow));
}
}
return 0;
}
/**
* Convert a numerical value to the readable string notated equivalent.
* The size must be a string to support large integers and floats.
*
* @param int|float $size
* @return string
*/
public static function bytesTo(num $size): string {
$sizes = ['YB', 'ZB', 'EB', 'PB', 'TB', 'GB', 'MB', 'KB', 'B'];
$total = count($sizes);
while ($total-- && $size >= 1024) {
$size /= 1024;
}
return $size . $sizes[$total];
}
/**
* Convert a number from one base to another.
*
* @param int|float $no
* @param int $fromBase
* @param int $toBase
* @return string
*/
public static function convert(mixed $no, int $fromBase, int $toBase): string {
if ($fromBase === $toBase) {
return (string) $no;
}
return base_convert($no, $fromBase, $toBase);
}
/**
* Convert a number to it's currency equivalent, respecting locale.
* Allow for overrides through an options array.
*
* @param int|float $number
* @param \Titon\Utility\OptionMap $options {
* @var string $thousands Character used for thousands place
* @var string $decimals Character used for decimal
* @var int $places Decimal (cent) limit
* @var string $code Currency code with replaceable hash
* @var string $dollar Dollar sign with replaceable hash
* @var string $cents Cent sign with replaceable hash
* @var string $use Whether to use dollar or code for formatting
* @var string $negative Negative sign with replaceable hash
* }
* @return string
*/
public static function currency(num $number, OptionMap $options = Map {}): string {
$options = (Map {
'thousands' => ',',
'decimals' => '.',
'places' => 2,
'code' => 'USD #',
'dollar' => '$#',
'cents' => '#¢',
'use' => 'dollar',
'negative' => '(#)'
})->setAll($options);
$amount = number_format(static::precision(abs($number), (int) $options['places']), $options['places'], $options['decimals'], $options['thousands']);
// Cents
if (($number < 1 && $number > -1) && $options['cents']) {
$amount = str_replace('#', $amount, $options['cents']);
// Dollars
} else {
if ($options['use'] === 'dollar') {
$amount = str_replace('#', $amount, $options['dollar']);
} else {
$amount = str_replace('#', $amount, $options['code']);
}
}
// Negative
if ($number < 0 && $options['negative']) {
$amount = str_replace('#', $amount, $options['negative']);
}
return $amount;
}
/**
* Return true if the number is within the min and max.
*
* @param int|float $number
* @param int|float $min
* @param int|float $max
* @return bool
*/
public static function in(num $number, num $min, num $max): bool {
return ($number >= $min && $number <= $max);
}
/**
* Is the current value even?
*
* @param int $number
* @return bool
*/
public static function isEven(int $number): bool {
return ($number % 2 === 0);
}
/**
* Is the current value negative; less than zero.
*
* @param int|float $number
* @return bool
*/
public static function isNegative(num $number): bool {
return ($number < 0);
}
/**
* Is the current value odd?
*
* @param int $number
* @return bool
*/
public static function isOdd(int $number): bool {
return !static::isEven($number);
}
/**
* Is the current value positive; greater than or equal to zero.
*
* @param int|float $number
* @param bool $zero
* @return bool
*/
public static function isPositive(num $number, bool $zero = true): bool {
return ($zero ? ($number >= 0) : ($number > 0));
}
/**
* Limits the number between two bounds.
*
* @param int|float $number
* @param int|float $min
* @param int|float $max
* @return int|float
*/
public static function limit(num $number, num $min, num $max): num {
return static::max(static::min($number, $min), $max);
}
/**
* Increase the number to the minimum if below threshold.
*
* @param int|float $number
* @param int|float $min
* @return int|float
*/
public static function min(num $number, num $min): num {
return ($number < $min) ? $min : $number;
}
/**
* Decrease the number to the maximum if above threshold.
*
* @param int|float $number
* @param int|float $max
* @return int|float
*/
public static function max(num $number, num $max): num {
return ($number > $max) ? $max : $number;
}
/**
* Return true if the number is outside the min and max.
*
* @param int|float $number
* @param int|float $min
* @param int|float $max
* @return bool
*/
public static function out(num $number, num $min, num $max): bool {
return ($number < $min || $number > $max);
}
/**
* Convert a number to a percentage string with decimal and comma separations.
*
* @param int|float $number
* @param \Titon\Utility\OptionMap $options {
* @var string $thousands Character used for thousands place
* @var string $decimals Character used for decimal
* @var int $places Decimal (cent) limit
* }
* @return string
*/
public static function percentage(num $number, OptionMap $options = Map {}): string {
$options = (Map {
'thousands' => ',',
'decimals' => '.',
'places' => 2
})->setAll($options);
return number_format(static::precision($number, (int) $options['places']), $options['places'], $options['decimals'], $options['thousands']) . '%';
}
/**
* Formats a number with a level of precision (even if it had none).
*
* @param int|float $number
* @param int $precision
* @return float
*/
public static function precision(num $number, int $precision = 2): float {
$float = $number;
// Hack's type checker doesn't allow variable sprintf() arguments, only literal strings.
// So we have to use this approach. Let's hope no one needs an outlandish precision.
switch ($precision) {
case 1: $float = sprintf("%01.1f", $number); break;
default:
case 2: $float = sprintf("%01.2f", $number); break;
case 3: $float = sprintf("%01.3f", $number); break;
case 4: $float = sprintf("%01.4f", $number); break;
case 5: $float = sprintf("%01.5f", $number); break;
}
return (float) $float;
}
/**
* Returns -1 if the value is negative, 0 if the value equals 0, or 1 if the value is positive.
*
* @param int|float $number
* @return int
*/
public static function signum(num $number): int {
if ($number < 0) {
return -1;
} else if ($number == 0) {
return 0;
} else {
return 1;
}
}
/**
* Returns as an unsigned integer in base 2 (binary).
*
* @param int|float $number
* @param int $base
* @return string
*/
public static function toBinary(mixed $number, int $base = self::DECIMAL): string {
return static::convert($number, $base, self::BINARY);
}
/**
* Returns as an unsigned integer in base 10 (decimal).
*
* @param int|float $number
* @param int $base
* @return string
*/
public static function toDecimal(mixed $number, int $base = self::DECIMAL): string {
return static::convert($number, $base, self::DECIMAL);
}
/**
* Returns as an unsigned integer in base 16 (hexadecimal).
*
* @param int|float $number
* @param int $base
* @return string
*/
public static function toHex(mixed $number, int $base = self::DECIMAL): string {
return static::convert($number, $base, self::HEX);
}
/**
* Returns as an unsigned integer in base 8 (octal).
*
* @param int|float $number
* @param int $base
* @return string
*/
public static function toOctal(mixed $number, int $base = self::DECIMAL): string {
return static::convert($number, $base, self::OCTAL);
}
}