Skip to content
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

[10.x] Add a Number utility class #48845

Merged
merged 21 commits into from
Nov 15, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
2 changes: 1 addition & 1 deletion src/Illuminate/Support/Number.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public static function toHuman($number, $locale = 'en')
*/
public static function bytesToHuman($bytes, $precision = 2)
{
$units = ['B', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'];
$units = ['B', 'kB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'];
Copy link
Contributor Author

@caendesilva caendesilva Nov 1, 2023

Choose a reason for hiding this comment

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

There has been some discussion whether we should use kB or KB.

It's worth noting that Rails uses KB. https://api.rubyonrails.org/classes/ActionView/Helpers/NumberHelper.html


for ($i = 0; ($bytes / 1024) > 0.9 && ($i < count($units) - 1); $i++) {
$bytes /= 1024;
Expand Down
8 changes: 4 additions & 4 deletions tests/Support/SupportNumberTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,10 @@ public function testBytesToHuman()
{
$this->assertSame('0 B', Number::bytesToHuman(0));
$this->assertSame('1 B', Number::bytesToHuman(1));
$this->assertSame('1 KB', Number::bytesToHuman(1024));
$this->assertSame('2 KB', Number::bytesToHuman(2048));
$this->assertSame('1.23 KB', Number::bytesToHuman(1264));
$this->assertSame('1.234 KB', Number::bytesToHuman(1264, 3));
$this->assertSame('1 kB', Number::bytesToHuman(1024));
$this->assertSame('2 kB', Number::bytesToHuman(2048));
$this->assertSame('1.23 kB', Number::bytesToHuman(1264));
$this->assertSame('1.234 kB', Number::bytesToHuman(1264, 3));
$this->assertSame('5 GB', Number::bytesToHuman(1024 * 1024 * 1024 * 5));
$this->assertSame('10 TB', Number::bytesToHuman((1024 ** 4) * 10));
$this->assertSame('10 PB', Number::bytesToHuman((1024 ** 5) * 10));
Expand Down
Loading