8000 [12.x] Update "Number::fileSize" to use correct prefix and add prefix… · laravel/framework@58ce619 · GitHub
[go: up one dir, main page]

Skip to content

Commit 58ce619

Browse files
Boy132taylorotwell
andauthored
[12.x] Update "Number::fileSize" to use correct prefix and add prefix param (#55678)
* update "Number::fileSize" to use correct prefix and add prefix param * fix style * fix tests * formatting --------- Co-authored-by: Taylor Otwell <taylor@laravel.com>
1 parent d100d1c commit 58ce619

File tree

2 files changed

+41
-16
lines changed

2 files changed

+41
-16
lines changed

src/Illuminate/Support/Number.php

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -160,14 +160,19 @@ public static function currency(int|float $number, string $in = '', ?string $loc
160160
* @param int|float $bytes
161161
* @param int $precision
162162
* @param int|null $maxPrecision
163+
* @param bool $useBinaryPrefix
163164
* @return string
164165
*/
165-
public static function fileSize(int|float $bytes, int $precision = 0, ?int $maxPrecision = null)
166+
public static function fileSize(int|float $bytes, int $precision = 0, ?int $maxPrecision = null, bool $useBinaryPrefix = false)
166167
{
167-
$units = ['B', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'];
168+
$base = $useBinaryPrefix ? 1024 : 1000;
168169

169-
for ($i = 0; ($bytes / 1024) > 0.9 && ($i < count($units) - 1); $i++) {
170-
$bytes /= 1024;
170+
$units = $useBinaryPrefix
171+
? ['B', 'KiB', 'MiB', 'GiB', 'TiB', 'PiB', 'EiB', 'ZiB', 'YiB', 'RiB', 'QiB']
172+
: ['B', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB', 'RB', 'QB'];
173+
174+
for ($i = 0; ($bytes / $base) > 0.9 && ($i < count($units) - 1); $i++) {
175+
$bytes /= $base;
171176
}
172177

173178
return sprintf('%s %s', static::format($bytes, $precision, $maxPrecision), $units[$i]);

tests/Support/SupportNumberTest.php

Lines changed: 32 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -174,18 +174,38 @@ public function testBytesToHuman()
174174
$this->assertSame('0 B', Number::fileSize(0));
175175
$this->assertSame('0.00 B', Number::fileSize(0, precision: 2));
176176
$this->assertSame('1 B', Number::fileSize(1));
177-
$this->assertSame('1 KB', Number::fileSize(1024));
178-
$this->assertSame('2 KB', Number::fileSize(2048));
179-
$this->assertSame('2.00 KB', Number::fileSize(2048, precision: 2));
180-
$this->assertSame('1.23 KB', Number::fileSize(1264, precision: 2));
181-
$this->assertSame('1.234 KB', Number::fileSize(1264.12345, maxPrecision: 3));
182-
$this->assertSame('1.234 KB', Number::fileSize(1264, 3));
183-
$this->assertSame('5 GB', Number::fileSize(1024 * 1024 * 1024 * 5));
184-
$this->assertSame('10 TB', Number::fileSize((1024 ** 4) * 10));
185-
$this->assertSame('10 PB', Number::fileSize((1024 ** 5) * 10));
186-
$this->assertSame('1 ZB', Number::fileSize(1024 ** 7));
187-
$this->assertSame('1 YB', Number::fileSize(1024 ** 8));
188-
$this->assertSame('1,024 YB', Number::fileSize(1024 ** 9));
177+
$this->assertSame('1 KB', Number::fileSize(1000));
178+
$this->assertSame('2 KB', Number::fileSize(2000));
179+
$this->assertSame('2.00 KB', Number::fileSize(2000, precision: 2));
180+
$this->assertSame('1.23 KB', Number::fileSize(1234, precision: 2));
181+
$this->assertSame('1.234 KB', Number::fileSize(1234, maxPrecision: 3));
182+
$this->assertSame('1.234 KB', Number::fileSize(1234, 3));
183+
$this->assertSame('5 GB', Number::fileSize(1000 * 1000 * 1000 * 5));
184+
$this->assertSame('10 TB', Number::fileSize((1000 ** 4) * 10));
185+
$this->assertSame('10 PB', Number::fileSize((1000 ** 5) * 10));
186+
$this->assertSame('1 ZB', Number::fileSize(1000 ** 7));
187+
$this->assertSame('1 YB', Number::fileSize(1000 ** 8));
188+
$this->assertSame('1 RB', Number::fileSize(1000 ** 9));
189+
$this->assertSame('1 QB', Number::fileSize(1000 ** 10));
190+
$this->assertSame('1,000 QB', Number::fileSize(1000 ** 11));
191+
192+
$this->assertSame('0 B', Number::fileSize(0, useBinaryPrefix: true));
193+
$this->assertSame('0.00 B', Number::fileSize(0, precision: 2, useBinaryPrefix: true));
194+
$this->assertSame('1 B', Number::fileSize(1, useBinaryPrefix: true));
195+
$this->assertSame('1 KiB', Number::fileSize(1024, useBinaryPrefix: true));
196+
$this->assertSame('2 KiB', Number::fileSize(2048, useBinaryPrefix: true));
197+
$this->assertSame('2.00 KiB', Number::fileSize(2048, precision: 2, useBinaryPrefix: true));
198+
$this->assertSame('1.23 KiB', Number::fileSize(1264, precision: 2, useBinaryPrefix: true));
199+
$this->assertSame('1.234 KiB', Number::fileSize(1264.12345, maxPrecision: 3, useBinaryPrefix: true));
200+
$this->assertSame('1.234 KiB', Number::fileSize(1264, 3, useBinaryPrefix: true));
201+
$this->assertSame('5 GiB', Number::fileSize(1024 * 1024 * 1024 * 5, useBinaryPrefix: true));
202+
$this->assertSame('10 TiB', Number::fileSize((1024 ** 4) * 10, useBinaryPrefix: true));
203+
$this->assertSame('10 PiB', Number::fileSize((1024 ** 5) * 10, useBinaryPrefix: true));
204+
$this->assertSame('1 ZiB', Number::fileSize(1024 ** 7, useBinaryPrefix: true));
205+
$this->assertSame('1 YiB', Number::fileSize(1024 ** 8, useBinaryPrefix: true));
206+
$this->assertSame('1 RiB', Number::fileSize(1024 ** 9, useBinaryPrefix: true));
207+
$this->assertSame('1 QiB', Number::fileSize(1024 ** 10, useBinaryPrefix: true));
208+
$this->assertSame('1,024 QiB', Number::fileSize(1024 ** 11, useBinaryPrefix: true));
189209
}
190210

191211
public function testClamp()

0 commit comments

Comments
 (0)
0