Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions src/Illuminate/Support/Number.php
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,27 @@ public static function fileSize(int|float $bytes, int $precision = 0, ?int $maxP
return sprintf('%s %s', static::format($bytes, $precision, $maxPrecision), $units[$i]);
}

/**
* Convert the given number to its bit rate equivalent.
*
* @param int|float $bits
* @param int $precision
* @param int|null $maxPrecision
* @return string
*/
public static function bitRate(int|float $bits, int $precision = 0, ?int $maxPrecision = null)
{
$units = ['bps', 'Kbps', 'Mbps', 'Gbps', 'Tbps', 'Pbps', 'Ebps', 'Zbps', 'Ybps'];

$unitCount = count($units);

for ($i = 0; ($bits / 1000) > 0.9 && ($i < $unitCount - 1); $i++) {
$bits /= 1000;
}

return sprintf('%s %s', static::format($bits, $precision, $maxPrecision), $units[$i]);
Comment on lines +229 to +237
Copy link
Contributor

@shaedrich shaedrich Oct 7, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You could reduce duplicated code

Suggested change
$units = ['bps', 'Kbps', 'Mbps', 'Gbps', 'Tbps', 'Pbps', 'Ebps', 'Zbps', 'Ybps'];
$unitCount = count($units);
for ($i = 0; ($bits / 1000) > 0.9 && ($i < $unitCount - 1); $i++) {
$bits /= 1000;
}
return sprintf('%s %s', static::format($bits, $precision, $maxPrecision), $units[$i]);
$formatted = strtolower(static::fileSize($bits, $precision, $maxPrexision) . 'ps';
return $bits < 1000 ? $formatted : ucwords($formatted);

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@shaedrich I didn't reuse the fileSize() method because bits and bytes are calculated differently. Bytes are calculated using binary base-2 (power of 1024) where bits are decimal base-10 (power of 1000).

Also the capitalization of the "b" matters as "Kb" = Kilobit and "KB" is Kilobyte etc.

Copy link
Contributor

@shaedrich shaedrich Oct 7, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Capitalization is taken care of as you can see, but you are right about the base.

This could probably be moved to a private utility method:

    /**
	 * @template TValue of string[]
	 * 
	 * @param TValue $values
	 *
     * @return array{int|float, value-of<TValue>}
     */
    private static function convertForDisplay(int|float $value, array $units, int $base)
    {
        $unitCount = count($units);

        for ($i = 0; ($value / $base) > 0.9 && ($i < $unitCount - 1); $i++) {
            $value /= $base;
        }

        return [$value, $units[$i]];
    }

but maybe, this is overkill

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My 2c I think the utility method might be useful if there is a 3rd method that relies on converting an integer to a string with a unit. Just my personal take 🤷‍♂️

Happy to do that in a separate PR and apply that to the fileSize() method as well.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Absolutely 👍🏻

}

/**
* Convert the number to its human-readable equivalent.
*
Expand Down
23 changes: 23 additions & 0 deletions tests/Support/SupportNumberTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,29 @@ public function testBytesToHuman()
$this->assertSame('1,024 YB', Number::fileSize(1024 ** 9));
}

public function testBitRate()
{
$this->assertSame('0 bps', Number::bitRate(0));
$this->assertSame('0.00 bps', Number::bitRate(0, precision: 2));
$this->assertSame('1 bps', Number::bitRate(1));
$this->assertSame('900 bps', Number::bitRate(900));
$this->assertSame('1 Kbps', Number::bitRate(1000));
$this->assertSame('2 Kbps', Number::bitRate(2000));
$this->assertSame('2.00 Kbps', Number::bitRate(2000, precision: 2));
$this->assertSame('1.25 Kbps', Number::bitRate(1250, precision: 2));
$this->assertSame('1.235 Kbps', Number::bitRate(1234.56789, maxPrecision: 3));
$this->assertSame('1.235 Kbps', Number::bitRate(1234.56789, 3));
$this->assertSame('1 Mbps', Number::bitRate(1000 * 1000));
$this->assertSame('1 Gbps', Number::bitRate(1000 * 1000 * 1000));
$this->assertSame('5 Gbps', Number::bitRate(1000 * 1000 * 1000 * 5));
$this->assertSame('10 Tbps', Number::bitRate((1000 ** 4) * 10));
$this->assertSame('10 Pbps', Number::bitRate((1000 ** 5) * 10));
$this->assertSame('1 Ebps', Number::bitRate(1000 ** 6));
$this->assertSame('1 Zbps', Number::bitRate(1000 ** 7));
$this->assertSame('1 Ybps', Number::bitRate(1000 ** 8));
$this->assertSame('1,000 Ybps', Number::bitRate(1000 ** 9));
}

public function testClamp()
{
$this->assertSame(2, Number::clamp(1, 2, 3));
Expand Down