formatBytesToHuman function

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

Implementation

String formatBytesToHuman(int bytes, {int decimals = 1}) {
  if (bytes < 0) return '-${formatBytesToHuman(-bytes, decimals: decimals)}';
  if (bytes == 0) return _kSizeZeroB;
  int i = 0;
  double v = bytes.toDouble();
  while (v >= 1024 && i < _sizeSuffixes.length - 1) {
    v /= 1024;
    i++;
  }
  final String formatted = v >= 10 || v == v.truncateToDouble()
      ? v.truncate().toString()
      : v.toStringAsFixed(decimals).replaceAll(RegExp(r'0+$'), '').replaceAll(RegExp(r'\.$'), '');
  return '$formatted ${_sizeSuffixes[i]}';
}