bytesAsReadable method

String bytesAsReadable(
  1. int bytes, {
  2. bool pad = true,
})

returns the the number of bytes in a human readable form. e.g. 1e+5 3.000T, 10.00G, 100.0M, 20.00K, 10B

Except for absurdly large no. (> 10^20) the return is guarenteed to be 6 characters long. For no. < 1000K we right pad the no. with spaces.

Implementation

String bytesAsReadable(int bytes, {bool pad = true}) {
  String human;

  if (bytes < 1000) {
    human = _fiveDigits(bytes, 0, 'B', pad: pad);
  } else if (bytes < 1000000) {
    human = _fiveDigits(bytes, 3, 'K', pad: pad);
  } else if (bytes < 1000000000) {
    human = _fiveDigits(bytes, 6, 'M', pad: pad);
  } else if (bytes < 1000000000000) {
    human = _fiveDigits(bytes, 9, 'G', pad: pad);
  } else if (bytes < 1000000000000000) {
    human = _fiveDigits(bytes, 12, 'T', pad: pad);
  } else {
    human = bytes.toStringAsExponential(0);
  }
  return human;
}