ente/lib/utils/data_util.dart

26 lines
781 B
Dart
Raw Normal View History

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(
(bytes / (1024 * 1024 * 1024)).toStringAsFixed(precision),);
2021-02-01 11:02:30 +00:00
}
2021-03-02 06:35:10 +00:00
2021-07-27 14:19:58 +00:00
final kStorageUnits = ["bytes", "KB", "MB", "GB"];
2021-03-02 06:35:10 +00:00
String convertBytesToReadableFormat(int bytes) {
int storageUnitIndex = 0;
2021-07-27 14:19:58 +00:00
while (bytes >= 1024 && storageUnitIndex < kStorageUnits.length - 1) {
2021-03-02 06:35:10 +00:00
storageUnitIndex++;
bytes = (bytes / 1024).round();
}
return bytes.toString() + " " + kStorageUnits[storageUnitIndex];
}
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;
int dm = decimals < 0 ? 0 : decimals;
int i = (log(bytes) / log(k)).floor();
return ((bytes / pow(k, i)).toStringAsFixed(dm)) + ' ' + kStorageUnits[i];
}