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, ...
This commit is contained in:
NBA2K1 2025-12-17 21:24:26 +01:00
parent 0ed8ee2cd2
commit a078b59678

View file

@ -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]}";
}
}