Skip to content

Commit

Permalink
Add support for Number::summarize (#475)
Browse files Browse the repository at this point in the history
* Add support for `Number::summarize`

Co-Authored-By: Deeka Wong <8337659+huangdijia@users.noreply.github.com>

* Format

Co-Authored-By: Deeka Wong <8337659+huangdijia@users.noreply.github.com>

---------

Co-authored-by: Deeka Wong <8337659+huangdijia@users.noreply.github.com>
  • Loading branch information
huangdijia and huangdijia authored Nov 30, 2023
1 parent 98c117f commit e54872e
Showing 1 changed file with 59 additions and 28 deletions.
87 changes: 59 additions & 28 deletions src/Number.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ class Number
/**
* Format the given number according to the current locale.
*
* @param ?string $locale
* @return string|false
*/
public static function format(int|float $number, ?int $precision = null, ?int $maxPrecision = null, ?string $locale = null)
Expand All @@ -52,7 +51,6 @@ public static function format(int|float $number, ?int $precision = null, ?int $m
/**
* Spell out the given number in the given locale.
*
* @param ?string $locale
* @return string
*/
public static function spell(int|float $number, ?string $locale = null)
Expand All @@ -67,7 +65,6 @@ public static function spell(int|float $number, ?string $locale = null)
/**
* Convert the given number to ordinal form.
*
* @param ?string $locale
* @return string
*/
public static function ordinal(int|float $number, ?string $locale = null)
Expand All @@ -82,7 +79,6 @@ public static function ordinal(int|float $number, ?string $locale = null)
/**
* Convert the given number to its percentage equivalent.
*
* @param ?string $locale
* @return string|false
*/
public static function percentage(int|float $number, int $precision = 0, ?int $maxPrecision = null, ?string $locale = null)
Expand All @@ -103,7 +99,6 @@ public static function percentage(int|float $number, int $precision = 0, ?int $m
/**
* Convert the given number to its currency equivalent.
*
* @param ?string $locale
* @return string|false
*/
public static function currency(int|float $number, string $in = 'USD', ?string $locale = null)
Expand Down Expand Up @@ -137,30 +132,32 @@ public static function fileSize(int|float $bytes, int $precision = 0, ?int $maxP
* @param int $number
* @return string
*/
public static function forHumans(int|float $number, int $precision = 0, ?int $maxPrecision = null)
public static function abbreviate(int|float $number, int $precision = 0, ?int $maxPrecision = null)
{
$units = [
3 => 'thousand',
6 => 'million',
9 => 'billion',
12 => 'trillion',
15 => 'quadrillion',
];

switch (true) {
case $number === 0:
return '0';
case $number < 0:
return sprintf('-%s', static::forHumans(abs($number), $precision, $maxPrecision));
case $number >= 1e15:
return sprintf('%s quadrillion', static::forHumans($number / 1e15, $precision, $maxPrecision));
}

$numberExponent = floor(log10($number));
$displayExponent = $numberExponent - ($numberExponent % 3);
$number /= pow(10, $displayExponent);
return static::forHumans($number, $precision, $maxPrecision, abbreviate: true);
}

return trim(sprintf('%s %s', static::format($number, $precision, $maxPrecision), $units[$displayExponent] ?? ''));
/**
* Convert the number to its human readable equivalent.
*
* @param int $number
* @return 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',
]);
}

/**
Expand All @@ -186,7 +183,41 @@ public static function useLocale(string $locale)
}

/**
* Ensure the "intl" PHP extension is installed.
* Convert the number to its human readable equivalent.
*
* @param int $number
* @return string
*/
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 $number === 0:
return '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] ?? ''));
}

/**
* Ensure the "intl" PHP exntension is installed.
*/
protected static function ensureIntlExtensionIsInstalled()
{
Expand Down

0 comments on commit e54872e

Please sign in to comment.