2025-01-16 21:20:44 -06:00
|
|
|
import 'dart:math';
|
|
|
|
|
2022-08-30 05:44:43 +02:00
|
|
|
String formatBytes(int bytes) {
|
2022-11-25 06:39:27 +13:00
|
|
|
const units = ['B', 'KiB', 'MiB', 'GiB', 'TiB', 'PiB', 'EiB'];
|
|
|
|
|
|
|
|
int magnitude = 0;
|
|
|
|
double remainder = bytes.toDouble();
|
|
|
|
while (remainder >= 1024) {
|
|
|
|
if (magnitude + 1 < units.length) {
|
|
|
|
magnitude++;
|
|
|
|
remainder /= 1024;
|
2024-01-27 16:14:32 +00:00
|
|
|
} else {
|
2022-11-25 06:39:27 +13:00
|
|
|
break;
|
|
|
|
}
|
2022-08-30 05:44:43 +02:00
|
|
|
}
|
2022-11-25 06:39:27 +13:00
|
|
|
|
|
|
|
return "${remainder.toStringAsFixed(magnitude == 0 ? 0 : 1)} ${units[magnitude]}";
|
|
|
|
}
|
2025-01-16 21:20:44 -06:00
|
|
|
|
|
|
|
String formatHumanReadableBytes(int bytes, int decimals) {
|
|
|
|
if (bytes <= 0) return "0 B";
|
|
|
|
const suffixes = ["B", "KB", "MB", "GB", "TB"];
|
|
|
|
var i = (log(bytes) / log(1024)).floor();
|
|
|
|
return '${(bytes / pow(1024, i)).toStringAsFixed(decimals)} ${suffixes[i]}';
|
|
|
|
}
|