Skip to content

Commit

Permalink
Display sizes like 1024 MB as 1 GB instead.
Browse files Browse the repository at this point in the history
  • Loading branch information
meisterT committed Mar 23, 2024
1 parent 113c18b commit e8ce88b
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 6 deletions.
10 changes: 5 additions & 5 deletions webapp/src/Utils/Utils.php
Original file line number Diff line number Diff line change
Expand Up @@ -430,18 +430,18 @@ public static function printhost(string $hostname, bool $full = false): string
}

/**
* Print (file) size in human readable format by using B,KB,MB,GB suffixes.
* Input is a integer (the size in bytes), output a string with suffix.
* Print (file) size in human-readable format by using B,KB,MB,GB suffixes.
* Input is an integer (the size in bytes), output a string with suffix.
*/
public static function printsize(int $size, int $decimals = 1): string
{
$factor = 1024;
$units = ['B', 'KB', 'MB', 'GB'];
$display = (int)$size;
$display = $size;

$exact = true;
for ($i = 0; $i < count($units) && $display > $factor; $i++) {
if (((int)$display % $factor)!=0) {
for ($i = 0; $i < count($units) && $display >= $factor; $i++) {
if (($display % $factor)!=0) {
$exact = false;
}
$display /= $factor;
Expand Down
5 changes: 4 additions & 1 deletion webapp/tests/Unit/Utils/UtilsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -505,8 +505,11 @@ public function testPrintsize(): void
{
self::assertEquals("0 B", Utils::printsize(0));
self::assertEquals("1000 B", Utils::printsize(1000));
self::assertEquals("1024 B", Utils::printsize(1024));
self::assertEquals("1023 B", Utils::printsize(1023));
self::assertEquals("1 KB", Utils::printsize(1024));
self::assertEquals("1.0 KB", Utils::printsize(1025));
self::assertEquals("1.0 KB", Utils::printsize(1075));
self::assertEquals("1.1 KB", Utils::printsize(1076));
self::assertEquals("2 KB", Utils::printsize(2048));
self::assertEquals("2.5 KB", Utils::printsize(2560));
self::assertEquals("5 MB", Utils::printsize(5242880));
Expand Down

0 comments on commit e8ce88b

Please sign in to comment.