Skip to content

Commit

Permalink
Move base units to a constant
Browse files Browse the repository at this point in the history
  • Loading branch information
shaedrich authored Jan 4, 2025
1 parent 8b93d39 commit 8707be0
Showing 1 changed file with 11 additions and 15 deletions.
26 changes: 11 additions & 15 deletions src/Illuminate/Support/Number.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,14 @@ class Number
{
use Macroable;

private const BASE_UNITS = [
3 => 'K',
6 => 'M',
9 => 'B',
12 => 'T',
15 => 'Q',
];

/**
* The current default locale.
*
Expand Down Expand Up @@ -192,13 +200,7 @@ public static function abbreviate(int|float $number, int $precision = 0, ?int $m
*/
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',
] : [
return static::summarize($number, $precision, $maxPrecision, $abbreviate ? self::BASE_UNITS : [
3 => ' thousand',
6 => ' million',
9 => ' billion',
Expand All @@ -218,14 +220,8 @@ public static function forHumans(int|float $number, int $precision = 0, ?int $ma
*/
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',
];
if ($units === []) {
$units = self::BASE_UNITS;
}

switch (true) {
Expand Down

0 comments on commit 8707be0

Please sign in to comment.