3
declare(strict_types=1);
16
if (! function_exists('number_to_size')) {
25
function number_to_size($num, int $precision = 1, ?string $locale = null)
30
$num = 0 + str_replace(',', '', (string) $num);
31
} catch (ErrorException) {
37
$generalLocale = $locale;
38
if ($locale !== null && $locale !== '' && ($underscorePos = strpos($locale, '_'))) {
39
$generalLocale = substr($locale, 0, $underscorePos);
42
if ($num >= 1_000_000_000_000) {
43
$num = round($num / 1_099_511_627_776, $precision);
44
$unit = lang('Number.terabyteAbbr', [], $generalLocale);
45
} elseif ($num >= 1_000_000_000) {
46
$num = round($num / 1_073_741_824, $precision);
47
$unit = lang('Number.gigabyteAbbr', [], $generalLocale);
48
} elseif ($num >= 1_000_000) {
49
$num = round($num / 1_048_576, $precision);
50
$unit = lang('Number.megabyteAbbr', [], $generalLocale);
51
} elseif ($num >= 1000) {
52
$num = round($num / 1024, $precision);
53
$unit = lang('Number.kilobyteAbbr', [], $generalLocale);
55
$unit = lang('Number.bytes', [], $generalLocale);
58
return format_number($num, $precision, $locale, ['after' => ' ' . $unit]);
62
if (! function_exists('number_to_amount')) {
80
function number_to_amount($num, int $precision = 0, ?string $locale = null)
85
$num = 0 + str_replace(',', '', (string) $num);
86
} catch (ErrorException) {
94
$generalLocale = $locale;
95
if ($locale !== null && $locale !== '' && ($underscorePos = strpos($locale, '_'))) {
96
$generalLocale = substr($locale, 0, $underscorePos);
99
if ($num >= 1_000_000_000_000_000) {
100
$suffix = lang('Number.quadrillion', [], $generalLocale);
101
$num = round(($num / 1_000_000_000_000_000), $precision);
102
} elseif ($num >= 1_000_000_000_000) {
103
$suffix = lang('Number.trillion', [], $generalLocale);
104
$num = round(($num / 1_000_000_000_000), $precision);
105
} elseif ($num >= 1_000_000_000) {
106
$suffix = lang('Number.billion', [], $generalLocale);
107
$num = round(($num / 1_000_000_000), $precision);
108
} elseif ($num >= 1_000_000) {
109
$suffix = lang('Number.million', [], $generalLocale);
110
$num = round(($num / 1_000_000), $precision);
111
} elseif ($num >= 1000) {
112
$suffix = lang('Number.thousand', [], $generalLocale);
113
$num = round(($num / 1000), $precision);
116
return format_number($num, $precision, $locale, ['after' => $suffix]);
120
if (! function_exists('number_to_currency')) {
121
function number_to_currency(float $num, string $currency, ?string $locale = null, int $fraction = 0): string
123
return format_number($num, 1, $locale, [
124
'type' => NumberFormatter::CURRENCY,
125
'currency' => $currency,
126
'fraction' => $fraction,
131
if (! function_exists('format_number')) {
136
function format_number(float $num, int $precision = 1, ?string $locale = null, array $options = []): string
140
$locale ??= Locale::getDefault();
143
$type = (int) ($options['type'] ?? NumberFormatter::DECIMAL);
145
$formatter = new NumberFormatter($locale, $type);
148
if ($type === NumberFormatter::CURRENCY) {
149
$formatter->setAttribute(NumberFormatter::FRACTION_DIGITS, (float) $options['fraction']);
150
$output = $formatter->formatCurrency($num, $options['currency']);
154
$pattern = '#,##0.' . str_repeat('#', $precision);
156
$formatter->setPattern($pattern);
157
$output = $formatter->format($num);
161
$output = trim($output, '. ');
163
if (intl_is_failure($formatter->getErrorCode())) {
164
throw new BadFunctionCallException($formatter->getErrorMessage());
168
if (isset($options['before']) && is_string($options['before'])) {
169
$output = $options['before'] . $output;
172
if (isset($options['after']) && is_string($options['after'])) {
173
$output .= $options['after'];
180
if (! function_exists('number_to_roman')) {
186
function number_to_roman($num): ?string
206
if ($num < 1 || $num > 3999) {
212
foreach ($map as $roman => $arabic) {
213
$repeat = (int) floor($num / $arabic);
214
$result .= str_repeat($roman, $repeat);