sizeLabel static method

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

Formats a byte count as a human-readable size.

FileUtils.sizeLabel(1536) // '1.5 KB'

Implementation

static String sizeLabel(int bytes, {int decimals = 1}) {
  if (bytes < 1024) return '$bytes B';
  const List<String> units = <String>['KB', 'MB', 'GB', 'TB', 'PB'];
  double value = bytes.toDouble();
  int unit = -1;
  do {
    value /= 1024;
    unit++;
  } while (value >= 1024 && unit < units.length - 1);
  final String text = value.toStringAsFixed(decimals);
  return '$text ${units[unit]}';
}