mirror of
https://github.com/kodjodevf/mangayomi.git
synced 2026-04-21 20:12:00 +00:00
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:
parent
0ed8ee2cd2
commit
a078b59678
1 changed files with 8 additions and 3 deletions
|
|
@ -17,10 +17,15 @@ import 'package:path/path.dart' as p;
|
||||||
|
|
||||||
extension FileFormatter on num {
|
extension FileFormatter on num {
|
||||||
String formattedFileSize({bool base1024 = true}) {
|
String formattedFileSize({bool base1024 = true}) {
|
||||||
|
if (this <= 0) return "0.00 B";
|
||||||
final base = base1024 ? 1024 : 1000;
|
final base = base1024 ? 1024 : 1000;
|
||||||
if (this <= 0) return "0";
|
final units = base1024
|
||||||
final units = ["B", "kB", "MB", "GB", "TB"];
|
? ["B", "KiB", "MiB", "GiB", "TiB"]
|
||||||
int digitGroups = (log(this) / log(base)).round();
|
: ["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]}";
|
return "${NumberFormat("#,##0.#").format(this / pow(base, digitGroups))} ${units[digitGroups]}";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue