|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* This file is part of Blitz PHP framework. |
|
5
|
|
|
* |
|
6
|
|
|
* (c) 2022 Dimitri Sitchet Tomkeu <[email protected]> |
|
7
|
|
|
* |
|
8
|
|
|
* For the full copyright and license information, please view |
|
9
|
|
|
* the LICENSE file that was distributed with this source code. |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
/** |
|
13
|
|
|
* FONCTIONS DE CONVERSION DE NOMBRE |
|
14
|
|
|
* |
|
15
|
|
|
* @credit <a href="https://codeigniter.com">CodeIgniter 4.2 - number_helper</a> |
|
16
|
|
|
*/ |
|
17
|
|
|
if (! function_exists('number_to_size')) { |
|
18
|
|
|
/** |
|
19
|
|
|
* Formats a numbers as bytes, based on size, and adds the appropriate suffix |
|
20
|
|
|
* |
|
21
|
|
|
* @param mixed $num Will be cast as int |
|
22
|
|
|
* @param string $locale |
|
23
|
|
|
* |
|
24
|
|
|
* @return bool|string |
|
25
|
|
|
*/ |
|
26
|
|
|
function number_to_size($num, int $precision = 1, ?string $locale = null) |
|
27
|
|
|
{ |
|
28
|
|
|
// Strip any formatting & ensure numeric input |
|
29
|
|
|
try { |
|
30
|
|
|
$num = 0 + str_replace(',', '', $num); |
|
31
|
|
|
} catch (ErrorException $ee) { |
|
32
|
|
|
return false; |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
// ignore sub part |
|
36
|
|
|
$generalLocale = $locale; |
|
37
|
|
|
if (! empty($locale) && ($underscorePos = strpos($locale, '_'))) { |
|
38
|
|
|
$generalLocale = substr($locale, 0, $underscorePos); |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
if ($num >= 1_000_000_000_000) { |
|
|
|
|
|
|
42
|
|
|
$num = round($num / 1_099_511_627_776, $precision); |
|
43
|
|
|
$unit = lang('Number.terabyteAbbr', [], $generalLocale); |
|
44
|
|
|
} elseif ($num >= 1_000_000_000) { |
|
45
|
|
|
$num = round($num / 1_073_741_824, $precision); |
|
46
|
|
|
$unit = lang('Number.gigabyteAbbr', [], $generalLocale); |
|
47
|
|
|
} elseif ($num >= 1_000_000) { |
|
48
|
|
|
$num = round($num / 1_048_576, $precision); |
|
49
|
|
|
$unit = lang('Number.megabyteAbbr', [], $generalLocale); |
|
50
|
|
|
} elseif ($num >= 1000) { |
|
51
|
|
|
$num = round($num / 1024, $precision); |
|
52
|
|
|
$unit = lang('Number.kilobyteAbbr', [], $generalLocale); |
|
53
|
|
|
} else { |
|
54
|
|
|
$unit = lang('Number.bytes', [], $generalLocale); |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
return format_number($num, $precision, $locale, ['after' => ' ' . $unit]); |
|
58
|
|
|
} |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
if (! function_exists('number_to_amount')) { |
|
62
|
|
|
/** |
|
63
|
|
|
* Converts numbers to a more readable representation |
|
64
|
|
|
* when dealing with very large numbers (in the thousands or above), |
|
65
|
|
|
* up to the quadrillions, because you won't often deal with numbers |
|
66
|
|
|
* larger than that. |
|
67
|
|
|
* |
|
68
|
|
|
* It uses the "short form" numbering system as this is most commonly |
|
69
|
|
|
* used within most English-speaking countries today. |
|
70
|
|
|
* |
|
71
|
|
|
* @see https://simple.wikipedia.org/wiki/Names_for_large_numbers |
|
72
|
|
|
* |
|
73
|
|
|
* @param string $num |
|
74
|
|
|
* |
|
75
|
|
|
* @return bool|string |
|
76
|
|
|
*/ |
|
77
|
|
|
function number_to_amount($num, int $precision = 0, ?string $locale = null) |
|
78
|
|
|
{ |
|
79
|
|
|
// Strip any formatting & ensure numeric input |
|
80
|
|
|
try { |
|
81
|
|
|
$num = 0 + str_replace(',', '', $num); |
|
82
|
|
|
} catch (ErrorException $ee) { |
|
83
|
|
|
return false; |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
$suffix = ''; |
|
87
|
|
|
|
|
88
|
|
|
// ignore sub part |
|
89
|
|
|
$generalLocale = $locale; |
|
90
|
|
|
if (! empty($locale) && ($underscorePos = strpos($locale, '_'))) { |
|
91
|
|
|
$generalLocale = substr($locale, 0, $underscorePos); |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
if ($num > 1_000_000_000_000_000) { |
|
95
|
|
|
$suffix = lang('Number.quadrillion', [], $generalLocale); |
|
96
|
|
|
$num = round(($num / 1_000_000_000_000_000), $precision); |
|
97
|
|
|
} elseif ($num > 1_000_000_000_000) { |
|
98
|
|
|
$suffix = lang('Number.trillion', [], $generalLocale); |
|
99
|
|
|
$num = round(($num / 1_000_000_000_000), $precision); |
|
100
|
|
|
} elseif ($num > 1_000_000_000) { |
|
101
|
|
|
$suffix = lang('Number.billion', [], $generalLocale); |
|
102
|
|
|
$num = round(($num / 1_000_000_000), $precision); |
|
103
|
|
|
} elseif ($num > 1_000_000) { |
|
104
|
|
|
$suffix = lang('Number.million', [], $generalLocale); |
|
105
|
|
|
$num = round(($num / 1_000_000), $precision); |
|
106
|
|
|
} elseif ($num > 1000) { |
|
107
|
|
|
$suffix = lang('Number.thousand', [], $generalLocale); |
|
108
|
|
|
$num = round(($num / 1000), $precision); |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
return format_number($num, $precision, $locale, ['after' => $suffix]); |
|
112
|
|
|
} |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
if (! function_exists('number_to_currency')) { |
|
116
|
|
|
function number_to_currency(float $num, string $currency, ?string $locale = null, int $fraction = 0): string |
|
117
|
|
|
{ |
|
118
|
|
|
return format_number($num, 1, $locale, [ |
|
119
|
|
|
'type' => NumberFormatter::CURRENCY, |
|
120
|
|
|
'currency' => $currency, |
|
121
|
|
|
'fraction' => $fraction, |
|
122
|
|
|
]); |
|
123
|
|
|
} |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
|
|
if (! function_exists('format_number')) { |
|
127
|
|
|
/** |
|
128
|
|
|
* A general purpose, locale-aware, number_format method. |
|
129
|
|
|
* Used by all of the functions of the number_helper. |
|
130
|
|
|
*/ |
|
131
|
|
|
function format_number(float $num, int $precision = 1, ?string $locale = null, array $options = []): string |
|
132
|
|
|
{ |
|
133
|
|
|
// If locale is not passed, get from the default locale that is set from our config file |
|
134
|
|
|
// or set by HTTP content negotiation. |
|
135
|
|
|
$locale ??= Locale::getDefault(); |
|
136
|
|
|
|
|
137
|
|
|
// Type can be any of the NumberFormatter options, but provide a default. |
|
138
|
|
|
$type = (int) ($options['type'] ?? NumberFormatter::DECIMAL); |
|
139
|
|
|
|
|
140
|
|
|
$formatter = new NumberFormatter($locale, $type); |
|
141
|
|
|
|
|
142
|
|
|
// Try to format it per the locale |
|
143
|
|
|
if ($type === NumberFormatter::CURRENCY) { |
|
144
|
|
|
$formatter->setAttribute(NumberFormatter::FRACTION_DIGITS, $options['fraction']); |
|
145
|
|
|
$output = $formatter->formatCurrency($num, $options['currency']); |
|
146
|
|
|
} else { |
|
147
|
|
|
// In order to specify a precision, we'll have to modify |
|
148
|
|
|
// the pattern used by NumberFormatter. |
|
149
|
|
|
$pattern = '#,##0.' . str_repeat('#', $precision); |
|
150
|
|
|
|
|
151
|
|
|
$formatter->setPattern($pattern); |
|
152
|
|
|
$output = $formatter->format($num); |
|
153
|
|
|
} |
|
154
|
|
|
|
|
155
|
|
|
// This might lead a trailing period if $precision == 0 |
|
156
|
|
|
$output = trim($output, '. '); |
|
157
|
|
|
|
|
158
|
|
|
if (intl_is_failure($formatter->getErrorCode())) { |
|
159
|
|
|
throw new BadFunctionCallException($formatter->getErrorMessage()); |
|
160
|
|
|
} |
|
161
|
|
|
|
|
162
|
|
|
// Add on any before/after text. |
|
163
|
|
|
if (isset($options['before']) && is_string($options['before'])) { |
|
164
|
|
|
$output = $options['before'] . $output; |
|
165
|
|
|
} |
|
166
|
|
|
|
|
167
|
|
|
if (isset($options['after']) && is_string($options['after'])) { |
|
168
|
|
|
$output .= $options['after']; |
|
169
|
|
|
} |
|
170
|
|
|
|
|
171
|
|
|
return $output; |
|
172
|
|
|
} |
|
173
|
|
|
} |
|
174
|
|
|
|
|
175
|
|
|
if (! function_exists('number_to_roman')) { |
|
176
|
|
|
/** |
|
177
|
|
|
* Convert a number to a roman numeral. |
|
178
|
|
|
* |
|
179
|
|
|
* @param string $num it will convert to int |
|
180
|
|
|
*/ |
|
181
|
|
|
function number_to_roman(string $num): ?string |
|
182
|
|
|
{ |
|
183
|
|
|
static $map = [ |
|
184
|
|
|
'M' => 1000, |
|
185
|
|
|
'CM' => 900, |
|
186
|
|
|
'D' => 500, |
|
187
|
|
|
'CD' => 400, |
|
188
|
|
|
'C' => 100, |
|
189
|
|
|
'XC' => 90, |
|
190
|
|
|
'L' => 50, |
|
191
|
|
|
'XL' => 40, |
|
192
|
|
|
'X' => 10, |
|
193
|
|
|
'IX' => 9, |
|
194
|
|
|
'V' => 5, |
|
195
|
|
|
'IV' => 4, |
|
196
|
|
|
'I' => 1, |
|
197
|
|
|
]; |
|
198
|
|
|
|
|
199
|
|
|
$num = (int) $num; |
|
200
|
|
|
|
|
201
|
|
|
if ($num < 1 || $num > 3999) { |
|
202
|
|
|
return null; |
|
203
|
|
|
} |
|
204
|
|
|
|
|
205
|
|
|
$result = ''; |
|
206
|
|
|
|
|
207
|
|
|
foreach ($map as $roman => $arabic) { |
|
208
|
|
|
$repeat = (int) floor($num / $arabic); |
|
209
|
|
|
$result .= str_repeat($roman, $repeat); |
|
210
|
|
|
$num %= $arabic; |
|
211
|
|
|
} |
|
212
|
|
|
|
|
213
|
|
|
return $result; |
|
214
|
|
|
} |
|
215
|
|
|
} |
|
216
|
|
|
|