ente/lib/utils/data_util.dart
2022-10-26 17:51:16 +05:30

41 lines
1.2 KiB
Dart

import 'dart:math';
final storageUnits = ["bytes", "KB", "MB", "GB"];
String convertBytesToReadableFormat(int bytes) {
int storageUnitIndex = 0;
while (bytes >= 1024 && storageUnitIndex < storageUnits.length - 1) {
storageUnitIndex++;
bytes = (bytes / 1024).round();
}
return bytes.toString() + " " + storageUnits[storageUnitIndex];
}
String formatBytes(int bytes, [int decimals = 2]) {
if (bytes == 0) return '0 bytes';
const k = 1024;
final int dm = decimals < 0 ? 0 : decimals;
final int i = (log(bytes) / log(k)).floor();
return ((bytes / pow(k, i)).toStringAsFixed(dm)) + ' ' + storageUnits[i];
}
//shows 1st decimal only if less than 10GB & omits decimal if decimal is 0
num roundBytesUsedToGBs(int usedBytes, int freeSpace) {
const tenGBinBytes = 10737418240;
num bytesInGB = convertBytesToGBs(usedBytes);
if ((usedBytes >= tenGBinBytes && freeSpace >= tenGBinBytes) ||
bytesInGB % 1 == 0) {
bytesInGB = bytesInGB.truncate();
}
return bytesInGB;
}
//Eg: 0.3 GB, 11.0 GB, 532.3 GB
num convertBytesToGBs(int bytes) {
return num.parse((bytes / (pow(1024, 3))).toStringAsFixed(1));
}
int convertBytesToMBs(int bytes) {
return (bytes / pow(1024, 2)).round();
}