formatBytes function

String formatBytes(
  1. int size,
  2. [int fractionDigits = 2]
)

Returns text representation of a provided bytes value (e.g. 1kB, 1GB)

Implementation

String formatBytes(int size, [int fractionDigits = 2]) {
  if (size <= 0) return '0 B';
  final multiple = (log(size) / log(1024)).floor();
  return (size / pow(1024, multiple)).toStringAsFixed(fractionDigits) +
      ' ' +
      ['B', 'kB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'][multiple];
}