From 6eb5f20376f9dd8cc39596e3251496da2d7ad98e Mon Sep 17 00:00:00 2001 From: Tobias Werth Date: Sat, 23 Mar 2024 12:43:50 +0100 Subject: [PATCH] Display sizes like `1024 MB` as `1 GB` instead. --- webapp/src/Utils/Utils.php | 10 +++++----- webapp/tests/Unit/Utils/UtilsTest.php | 5 ++++- 2 files changed, 9 insertions(+), 6 deletions(-) diff --git a/webapp/src/Utils/Utils.php b/webapp/src/Utils/Utils.php index 046b4b65e6e..ec149e8d54b 100644 --- a/webapp/src/Utils/Utils.php +++ b/webapp/src/Utils/Utils.php @@ -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; diff --git a/webapp/tests/Unit/Utils/UtilsTest.php b/webapp/tests/Unit/Utils/UtilsTest.php index c6e61c9335d..15d60d8efb9 100644 --- a/webapp/tests/Unit/Utils/UtilsTest.php +++ b/webapp/tests/Unit/Utils/UtilsTest.php @@ -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));