formatBytes static method

String formatBytes(
  1. int bytes, {
  2. int decimals = 2,
})

Implementation

static String formatBytes(int bytes, {int decimals = 2}) {
  if (bytes <= 0) return "0 B";
  const suffixes = ["B", "KB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB"];
  var i = (math.log(bytes) / math.log(1024)).floor();
  var formattedValue = bytes / math.pow(1024, i);
  return '${formattedValue.toStringAsFixed(decimals)} ${suffixes[i]}';
}