From a078b59678edb7aef4071470b59c22421a06d540 Mon Sep 17 00:00:00 2001 From: NBA2K1 <78034913+NBA2K1@users.noreply.github.com> Date: Wed, 17 Dec 2025 21:24:26 +0100 Subject: [PATCH] Use floor() instead of round() - Using round() will push values up too early. Example: log(999) / log(1024) = 0.999 .round() = 1. Result: 0.98 kB instead of 999 B - Use correct units, as base1024 = KiB, MiB, etc. and base1000 = kB, MB, GB, ... --- lib/utils/extensions/others.dart | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/lib/utils/extensions/others.dart b/lib/utils/extensions/others.dart index e57a5638..c281c731 100644 --- a/lib/utils/extensions/others.dart +++ b/lib/utils/extensions/others.dart @@ -17,10 +17,15 @@ import 'package:path/path.dart' as p; extension FileFormatter on num { String formattedFileSize({bool base1024 = true}) { + if (this <= 0) return "0.00 B"; final base = base1024 ? 1024 : 1000; - if (this <= 0) return "0"; - final units = ["B", "kB", "MB", "GB", "TB"]; - int digitGroups = (log(this) / log(base)).round(); + final units = base1024 + ? ["B", "KiB", "MiB", "GiB", "TiB"] + : ["B", "kB", "MB", "GB", "TB"]; + int digitGroups = (log(this) / log(base)).floor().clamp( + 0, + units.length - 1, + ); return "${NumberFormat("#,##0.#").format(this / pow(base, digitGroups))} ${units[digitGroups]}"; } }