Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Revert "[12.x] Update "Number::fileSize" to use correct prefix and add prefix param" #55741

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
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
13 changes: 4 additions & 9 deletions 13 src/Illuminate/Support/Number.php
Original file line number Diff line number Diff line change
Expand Up @@ -201,19 +201,14 @@ public static function currency(int|float $number, string $in = '', ?string $loc
* @param int|float $bytes
* @param int $precision
* @param int|null $maxPrecision
* @param bool $useBinaryPrefix
* @return string
*/
public static function fileSize(int|float $bytes, int $precision = 0, ?int $maxPrecision = null, bool $useBinaryPrefix = false)
public static function fileSize(int|float $bytes, int $precision = 0, ?int $maxPrecision = null)
{
$base = $useBinaryPrefix ? 1024 : 1000;
$units = ['B', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'];

$units = $useBinaryPrefix
? ['B', 'KiB', 'MiB', 'GiB', 'TiB', 'PiB', 'EiB', 'ZiB', 'YiB', 'RiB', 'QiB']
: ['B', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB', 'RB', 'QB'];

for ($i = 0; ($bytes / $base) > 0.9 && ($i < count($units) - 1); $i++) {
$bytes /= $base;
for ($i = 0; ($bytes / 1024) > 0.9 && ($i < count($units) - 1); $i++) {
$bytes /= 1024;
}

return sprintf('%s %s', static::format($bytes, $precision, $maxPrecision), $units[$i]);
Expand Down
44 changes: 12 additions & 32 deletions 44 tests/Support/SupportNumberTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -174,38 +174,18 @@ public function testBytesToHuman()
$this->assertSame('0 B', Number::fileSize(0));
$this->assertSame('0.00 B', Number::fileSize(0, precision: 2));
$this->assertSame('1 B', Number::fileSize(1));
$this->assertSame('1 KB', Number::fileSize(1000));
$this->assertSame('2 KB', Number::fileSize(2000));
$this->assertSame('2.00 KB', Number::fileSize(2000, precision: 2));
$this->assertSame('1.23 KB', Number::fileSize(1234, precision: 2));
$this->assertSame('1.234 KB', Number::fileSize(1234, maxPrecision: 3));
$this->assertSame('1.234 KB', Number::fileSize(1234, 3));
$this->assertSame('5 GB', Number::fileSize(1000 * 1000 * 1000 * 5));
$this->assertSame('10 TB', Number::fileSize((1000 ** 4) * 10));
$this->assertSame('10 PB', Number::fileSize((1000 ** 5) * 10));
$this->assertSame('1 ZB', Number::fileSize(1000 ** 7));
$this->assertSame('1 YB', Number::fileSize(1000 ** 8));
$this->assertSame('1 RB', Number::fileSize(1000 ** 9));
$this->assertSame('1 QB', Number::fileSize(1000 ** 10));
$this->assertSame('1,000 QB', Number::fileSize(1000 ** 11));

$this->assertSame('0 B', Number::fileSize(0, useBinaryPrefix: true));
$this->assertSame('0.00 B', Number::fileSize(0, precision: 2, useBinaryPrefix: true));
$this->assertSame('1 B', Number::fileSize(1, useBinaryPrefix: true));
$this->assertSame('1 KiB', Number::fileSize(1024, useBinaryPrefix: true));
$this->assertSame('2 KiB', Number::fileSize(2048, useBinaryPrefix: true));
$this->assertSame('2.00 KiB', Number::fileSize(2048, precision: 2, useBinaryPrefix: true));
$this->assertSame('1.23 KiB', Number::fileSize(1264, precision: 2, useBinaryPrefix: true));
$this->assertSame('1.234 KiB', Number::fileSize(1264.12345, maxPrecision: 3, useBinaryPrefix: true));
$this->assertSame('1.234 KiB', Number::fileSize(1264, 3, useBinaryPrefix: true));
$this->assertSame('5 GiB', Number::fileSize(1024 * 1024 * 1024 * 5, useBinaryPrefix: true));
$this->assertSame('10 TiB', Number::fileSize((1024 ** 4) * 10, useBinaryPrefix: true));
$this->assertSame('10 PiB', Number::fileSize((1024 ** 5) * 10, useBinaryPrefix: true));
$this->assertSame('1 ZiB', Number::fileSize(1024 ** 7, useBinaryPrefix: true));
$this->assertSame('1 YiB', Number::fileSize(1024 ** 8, useBinaryPrefix: true));
$this->assertSame('1 RiB', Number::fileSize(1024 ** 9, useBinaryPrefix: true));
$this->assertSame('1 QiB', Number::fileSize(1024 ** 10, useBinaryPrefix: true));
$this->assertSame('1,024 QiB', Number::fileSize(1024 ** 11, useBinaryPrefix: true));
$this->assertSame('1 KB', Number::fileSize(1024));
$this->assertSame('2 KB', Number::fileSize(2048));
$this->assertSame('2.00 KB', Number::fileSize(2048, precision: 2));
$this->assertSame('1.23 KB', Number::fileSize(1264, precision: 2));
$this->assertSame('1.234 KB', Number::fileSize(1264.12345, maxPrecision: 3));
$this->assertSame('1.234 KB', Number::fileSize(1264, 3));
$this->assertSame('5 GB', Number::fileSize(1024 * 1024 * 1024 * 5));
$this->assertSame('10 TB', Number::fileSize((1024 ** 4) * 10));
$this->assertSame('10 PB', Number::fileSize((1024 ** 5) * 10));
$this->assertSame('1 ZB', Number::fileSize(1024 ** 7));
$this->assertSame('1 YB', Number::fileSize(1024 ** 8));
$this->assertSame('1,024 YB', Number::fileSize(1024 ** 9));
}

public function testClamp()
Expand Down
Morty Proxy This is a proxified and sanitized view of the page, visit original site.