From 6c2a2060866803fb117c963aac39af545e4c0236 Mon Sep 17 00:00:00 2001 From: Simon Rettberg Date: Wed, 29 May 2024 14:52:56 +0200 Subject: [inc/Util] filesize: Fix calculating number of decimals to display --- inc/util.inc.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/inc/util.inc.php b/inc/util.inc.php index 267a3971..2568011b 100644 --- a/inc/util.inc.php +++ b/inc/util.inc.php @@ -95,14 +95,14 @@ class Util * Convert given number to human-readable file size string. * Will append Bytes, KiB, etc. depending on magnitude of number. * - * @param float|int $bytes numeric value of the filesize to make readable + * @param mixed $bytes numeric value of the filesize to make readable * @param int $decimals number of decimals to show, -1 for automatic * @param int $shift how many units to skip, i.e. if you pass in KiB or MiB * @return string human-readable string representing the given file size */ public static function readableFileSize($bytes, int $decimals = -1, int $shift = 0): string { - // round doesn't reliably work for large floats, pick workaround depending on OS + // round doesn't reliably work for large floats, and we use strlen either way $bytes = sprintf('%u', $bytes); static $sz = array('Byte', 'KiB', 'MiB', 'GiB', 'TiB', 'PiB'); $factor = (int)floor((strlen($bytes) - 1) / 3); @@ -111,7 +111,7 @@ class Util } else { $bytes /= 1024 ** $factor; if ($decimals === -1) { - $decimals = 2 - strlen((string)floor($bytes)) - 1; + $decimals = 3 - strlen((string)floor($bytes)); } } return Dictionary::number((float)$bytes, $decimals) . "\xe2\x80\x89" . ($sz[$factor + $shift] ?? '#>PiB#'); -- cgit v1.2.3-55-g7522