fileSize method

String fileSize({
  1. int decimalDigits = 1,
})

Formats this number as a binary file size.

Treats the current numeric value as bytes.

Implementation

String fileSize({
  int decimalDigits = 1,
}) {
  if (this <= 0) return '0 B';

  const units = <String>[
    'B',
    'KB',
    'MB',
    'GB',
    'TB',
    'PB',
  ];

  final calculatedIndex =
      (math.log(this) / math.log(1024)).floor();

  final index = calculatedIndex.clamp(0, units.length - 1).toInt();
  final value = this / math.pow(1024, index);

  return '${value.toStringAsFixed(decimalDigits)} ${units[index]}';
}