|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Tempest\Support\Number; |
| 4 | + |
| 5 | +use NumberFormatter; |
| 6 | +use Tempest\Support\Currency; |
| 7 | +use Tempest\Support\Language\Locale; |
| 8 | +use Tempest\Support\Math; |
| 9 | + |
| 10 | +/** |
| 11 | + * Formats the given number. |
| 12 | + * |
| 13 | + * @see https://www.php.net/manual/en/class.numberformatter.php |
| 14 | + */ |
| 15 | +function format(int|float $number, ?int $precision = null, ?int $maxPrecision = null, ?Locale $locale = null): string|false |
| 16 | +{ |
| 17 | + $locale ??= Locale::ENGLISH; |
| 18 | + $formatter = new NumberFormatter($locale->value, NumberFormatter::DECIMAL); |
| 19 | + |
| 20 | + if (! is_null($maxPrecision)) { |
| 21 | + $formatter->setAttribute(NumberFormatter::MAX_FRACTION_DIGITS, $maxPrecision); |
| 22 | + } elseif (! is_null($precision)) { |
| 23 | + $formatter->setAttribute(NumberFormatter::FRACTION_DIGITS, $precision); |
| 24 | + } |
| 25 | + |
| 26 | + return $formatter->format($number); |
| 27 | +} |
| 28 | + |
| 29 | +/** |
| 30 | + * Spells out the given number in the given locale. |
| 31 | + * |
| 32 | + * @see https://www.php.net/manual/en/class.numberformatter.php |
| 33 | + */ |
| 34 | +function spell_out(int|float $number, ?Locale $locale = null, ?int $after = null, ?int $until = null): string|false |
| 35 | +{ |
| 36 | + $locale ??= Locale::ENGLISH; |
| 37 | + |
| 38 | + if (! is_null($after) && $number <= $after) { |
| 39 | + return namespace\format($number, locale: $locale); |
| 40 | + } |
| 41 | + |
| 42 | + if (! is_null($until) && $number >= $until) { |
| 43 | + return namespace\format($number, locale: $locale); |
| 44 | + } |
| 45 | + |
| 46 | + $formatter = new NumberFormatter($locale->value, NumberFormatter::SPELLOUT); |
| 47 | + |
| 48 | + return $formatter->format($number); |
| 49 | +} |
| 50 | + |
| 51 | +/** |
| 52 | + * Converts the given number to ordinal form. |
| 53 | + * |
| 54 | + * @see https://www.php.net/manual/en/class.numberformatter.php |
| 55 | + */ |
| 56 | +function to_ordinal(int|float $number, ?Locale $locale = null): string|false |
| 57 | +{ |
| 58 | + $locale ??= Locale::ENGLISH; |
| 59 | + $formatter = new NumberFormatter($locale->value, NumberFormatter::ORDINAL); |
| 60 | + |
| 61 | + return $formatter->format($number); |
| 62 | +} |
| 63 | + |
| 64 | +/** |
| 65 | + * Spells out the given number in the given locale in ordinal form. |
| 66 | + * |
| 67 | + * @see https://www.php.net/manual/en/class.numberformatter.php |
| 68 | + */ |
| 69 | +function to_spelled_ordinal(int|float $number, ?Locale $locale = null): string|false |
| 70 | +{ |
| 71 | + $locale ??= Locale::ENGLISH; |
| 72 | + $formatter = new NumberFormatter($locale->value, NumberFormatter::SPELLOUT); |
| 73 | + $formatter->setTextAttribute(NumberFormatter::DEFAULT_RULESET, '%spellout-ordinal'); |
| 74 | + |
| 75 | + return $formatter->format($number); |
| 76 | +} |
| 77 | + |
| 78 | +/** |
| 79 | + * Converts the given number to its percentage equivalent. |
| 80 | + */ |
| 81 | +function to_percentage(int|float $number, int $precision = 0, ?int $maxPrecision = null, ?Locale $locale = null): string|false |
| 82 | +{ |
| 83 | + $locale ??= Locale::ENGLISH; |
| 84 | + $formatter = new NumberFormatter($locale->value, NumberFormatter::PERCENT); |
| 85 | + |
| 86 | + if (! is_null($maxPrecision)) { |
| 87 | + $formatter->setAttribute(NumberFormatter::MAX_FRACTION_DIGITS, $maxPrecision); |
| 88 | + } else { |
| 89 | + $formatter->setAttribute(NumberFormatter::FRACTION_DIGITS, $precision); |
| 90 | + } |
| 91 | + |
| 92 | + return $formatter->format($number / 100); |
| 93 | +} |
| 94 | + |
| 95 | +/** |
| 96 | + * Converts the given number to its currency equivalent. |
| 97 | + */ |
| 98 | +function currency(int|float $number, Currency $currency, ?Locale $locale = null, ?int $precision = null): string|false |
| 99 | +{ |
| 100 | + $locale ??= Locale::ENGLISH; |
| 101 | + $formatter = new NumberFormatter($locale->value, NumberFormatter::CURRENCY); |
| 102 | + |
| 103 | + if (! is_null($precision)) { |
| 104 | + $formatter->setAttribute(NumberFormatter::FRACTION_DIGITS, $precision); |
| 105 | + } |
| 106 | + |
| 107 | + return $formatter->formatCurrency($number, $currency->name); |
| 108 | +} |
| 109 | + |
| 110 | +/** |
| 111 | + * Converts the given number of bytes to its human-readable file size equivalent. |
| 112 | + */ |
| 113 | +function to_file_size(int|float $bytes, int $precision = 0, ?int $maxPrecision = null, bool $useBinaryPrefix = false): string |
| 114 | +{ |
| 115 | + $base = $useBinaryPrefix ? 1024 : 1000; |
| 116 | + $units = $useBinaryPrefix |
| 117 | + ? ['B', 'KiB', 'MiB', 'GiB', 'TiB', 'PiB', 'EiB', 'ZiB', 'YiB', 'RiB', 'QiB'] |
| 118 | + : ['B', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB', 'RB', 'QB']; |
| 119 | + |
| 120 | + for ($i = 0; ($bytes / $base) > 0.9 && $i < (count($units) - 1); $i++) { |
| 121 | + $bytes /= $base; |
| 122 | + } |
| 123 | + |
| 124 | + return sprintf('%s %s', namespace\format($bytes, $precision, $maxPrecision), $units[$i]); |
| 125 | +} |
| 126 | + |
| 127 | +/** |
| 128 | + * Converts the number to its human-readable equivalent. |
| 129 | + */ |
| 130 | +function to_human_readable(int|float $number, int $precision = 0, ?int $maxPrecision = null, array $units = []): string|false |
| 131 | +{ |
| 132 | + if ($units === []) { |
| 133 | + $units = [ |
| 134 | + 3 => 'K', |
| 135 | + 6 => 'M', |
| 136 | + 9 => 'B', |
| 137 | + 12 => 'T', |
| 138 | + 15 => 'Q', |
| 139 | + ]; |
| 140 | + } |
| 141 | + |
| 142 | + switch (true) { |
| 143 | + case floatval($number) === 0.0: |
| 144 | + return $precision > 0 ? namespace\format(0, $precision, $maxPrecision) : '0'; |
| 145 | + case $number < 0: |
| 146 | + return sprintf('-%s', namespace\to_human_readable(Math\abs($number), $precision, $maxPrecision, $units)); |
| 147 | + case $number >= 1e15: |
| 148 | + return sprintf('%s' . end($units), namespace\to_human_readable($number / 1e15, $precision, $maxPrecision, $units)); |
| 149 | + } |
| 150 | + |
| 151 | + $numberExponent = Math\floor(Math\log($number, base: 10)); |
| 152 | + $displayExponent = $numberExponent - ($numberExponent % 3); |
| 153 | + $number /= 10 ** $displayExponent; |
| 154 | + |
| 155 | + return trim(sprintf('%s%s', namespace\format($number, $precision, $maxPrecision), $units[$displayExponent] ?? '')); |
| 156 | +} |
0 commit comments