data_util.dart 804 B

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