diff --git a/src/Illuminate/Support/Number.php b/src/Illuminate/Support/Number.php index 47d218ffcd47..a9e8097afbb0 100644 --- a/src/Illuminate/Support/Number.php +++ b/src/Illuminate/Support/Number.php @@ -124,10 +124,10 @@ public static function currency(int|float $number, string $in = 'USD', ?string $ */ public static function fileSize(int|float $bytes, int $precision = 0, ?int $maxPrecision = null) { - $units = ['B', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB']; + $units = ['B', 'kB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB']; - for ($i = 0; ($bytes / 1024) > 0.9 && ($i < count($units) - 1); $i++) { - $bytes /= 1024; + for ($i = 0; ($bytes / 1000) > 0.9 && ($i < count($units) - 1); $i++) { + $bytes /= 1000; } return sprintf('%s %s', static::format($bytes, $precision, $maxPrecision), $units[$i]); diff --git a/tests/Support/SupportNumberTest.php b/tests/Support/SupportNumberTest.php index e758ecabdede..30d250dcf852 100644 --- a/tests/Support/SupportNumberTest.php +++ b/tests/Support/SupportNumberTest.php @@ -141,18 +141,25 @@ 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(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('1 kB', Number::fileSize(1000)); + $this->assertSame('1 kB', Number::fileSize(1024)); + $this->assertSame('1.02 kB', Number::fileSize(1024, precision: 2)); + $this->assertSame('2 kB', Number::fileSize(2000)); + $this->assertSame('2.00 kB', Number::fileSize(2000, precision: 2)); + $this->assertSame('2 kB', Number::fileSize(2048)); + $this->assertSame('2.048 kB', Number::fileSize(2048, precision: 3)); + $this->assertSame('1.26 kB', Number::fileSize(1264, precision: 2)); + $this->assertSame('1.264 kB', Number::fileSize(1264.12345, maxPrecision: 3)); + $this->assertSame('1.264 kB', Number::fileSize(1264, 3)); + $this->assertSame('5 GB', Number::fileSize(1000 * 1000 * 1000 * 5)); + $this->assertSame('5.37 GB', Number::fileSize(1024 * 1024 * 1024 * 5, precision: 2)); $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('10 TB', Number::fileSize((1000 ** 4) * 10)); + $this->assertSame('11 TB', Number::fileSize((1024 ** 4) * 10)); + $this->assertSame('11 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)); + $this->assertSame('1,238 YB', Number::fileSize(1024 ** 9)); } public function testToHuman()