data_util.dart 748 B

12345678910111213141516171819202122232425
  1. import 'dart:math';
  2. double convertBytesToGBs(final int bytes, {int precision = 2}) {
  3. return double.parse(
  4. (bytes / (1024 * 1024 * 1024)).toStringAsFixed(precision));
  5. }
  6. final kStorageUnits = ["bytes", "KB", "MB", "GB", "TB"];
  7. String convertBytesToReadableFormat(int bytes) {
  8. int storageUnitIndex = 0;
  9. while (bytes >= 1024) {
  10. storageUnitIndex++;
  11. bytes = (bytes / 1024).round();
  12. }
  13. return bytes.toString() + " " + kStorageUnits[storageUnitIndex];
  14. }
  15. String formatBytes(int bytes, [int decimals = 2]) {
  16. if (bytes == 0) return '0 bytes';
  17. const k = 1024;
  18. int dm = decimals < 0 ? 0 : decimals;
  19. int i = (log(bytes) / log(k)).floor();
  20. return ((bytes / pow(k, i)).toStringAsFixed(dm)) + ' ' + kStorageUnits[i];
  21. }