2021-06-28 12:54:27 +00:00
|
|
|
import 'dart:math';
|
|
|
|
|
2021-02-01 11:02:30 +00:00
|
|
|
double convertBytesToGBs(final int bytes, {int precision = 2}) {
|
|
|
|
return double.parse(
|
2022-06-11 08:23:52 +00:00
|
|
|
(bytes / (1024 * 1024 * 1024)).toStringAsFixed(precision),
|
|
|
|
);
|
2021-02-01 11:02:30 +00:00
|
|
|
}
|
2021-03-02 06:35:10 +00:00
|
|
|
|
2022-09-20 12:48:15 +00:00
|
|
|
final storageUnits = ["bytes", "KB", "MB", "GB"];
|
2021-03-02 06:35:10 +00:00
|
|
|
|
|
|
|
String convertBytesToReadableFormat(int bytes) {
|
|
|
|
int storageUnitIndex = 0;
|
2022-09-20 12:48:15 +00:00
|
|
|
while (bytes >= 1024 && storageUnitIndex < storageUnits.length - 1) {
|
2021-03-02 06:35:10 +00:00
|
|
|
storageUnitIndex++;
|
|
|
|
bytes = (bytes / 1024).round();
|
|
|
|
}
|
2022-09-20 12:48:15 +00:00
|
|
|
return bytes.toString() + " " + storageUnits[storageUnitIndex];
|
2021-03-02 06:35:10 +00:00
|
|
|
}
|
2021-06-28 12:54:27 +00:00
|
|
|
|
|
|
|
String formatBytes(int bytes, [int decimals = 2]) {
|
2021-07-27 14:19:58 +00:00
|
|
|
if (bytes == 0) return '0 bytes';
|
|
|
|
const k = 1024;
|
2022-08-29 14:43:31 +00:00
|
|
|
final int dm = decimals < 0 ? 0 : decimals;
|
|
|
|
final int i = (log(bytes) / log(k)).floor();
|
2022-09-20 12:48:15 +00:00
|
|
|
return ((bytes / pow(k, i)).toStringAsFixed(dm)) + ' ' + storageUnits[i];
|
2021-07-27 14:19:58 +00:00
|
|
|
}
|
2022-10-18 12:55:09 +00:00
|
|
|
|
2022-10-19 04:25:34 +00:00
|
|
|
//shows decimals only if less than 10GB & omits decimal if decimal is 0
|
2022-10-18 13:56:17 +00:00
|
|
|
num convertBytesToGB(int bytes) {
|
2022-10-18 12:55:09 +00:00
|
|
|
const tenGBinBytes = 10737418240;
|
2022-10-18 13:56:17 +00:00
|
|
|
int precision = 0;
|
2022-10-18 12:55:09 +00:00
|
|
|
if (bytes < tenGBinBytes) {
|
2022-10-18 13:56:17 +00:00
|
|
|
precision = 1;
|
2022-10-18 12:55:09 +00:00
|
|
|
}
|
2022-10-18 13:56:17 +00:00
|
|
|
final bytesInGB =
|
|
|
|
num.parse((bytes / (pow(1024, 3))).toStringAsPrecision(precision));
|
2022-10-18 12:55:09 +00:00
|
|
|
return bytesInGB;
|
|
|
|
}
|
2022-10-19 04:25:34 +00:00
|
|
|
|
|
|
|
int convertBytesToMB(int bytes) {
|
|
|
|
return (bytes / pow(1024, 2)).round();
|
|
|
|
}
|