data_util.dart 794 B

1234567891011121314151617181920212223242526
  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. }
  7. final kStorageUnits = ["bytes", "KB", "MB", "GB"];
  8. String convertBytesToReadableFormat(int bytes) {
  9. int storageUnitIndex = 0;
  10. while (bytes >= 1024 && storageUnitIndex < kStorageUnits.length - 1) {
  11. storageUnitIndex++;
  12. bytes = (bytes / 1024).round();
  13. }
  14. return bytes.toString() + " " + kStorageUnits[storageUnitIndex];
  15. }
  16. String formatBytes(int bytes, [int decimals = 2]) {
  17. if (bytes == 0) return '0 bytes';
  18. const k = 1024;
  19. final int dm = decimals < 0 ? 0 : decimals;
  20. final int i = (log(bytes) / log(k)).floor();
  21. return ((bytes / pow(k, i)).toStringAsFixed(dm)) + ' ' + kStorageUnits[i];
  22. }