sizeToHumanReadable static method

String sizeToHumanReadable(
  1. int size
)

Implementation

static String sizeToHumanReadable(int size) {
  const suffixes = ["B", "KB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB"];
  if (size <= 0 || size.isNaN) return '0 B';

  var i = size > 0 ? (log(size) / log(1024)).floor() : 0;
  i = i.clamp(0, suffixes.length - 1);

  double displaySize = size / pow(1024, i);
  if (displaySize.isInfinite || displaySize.isNaN) {
    return '0 B';
  }

  return '${displaySize.toStringAsFixed(2)} ${suffixes[i]}';
}