formatBytes function

String formatBytes(
  1. num bytes, {
  2. ByteUnits byteUnits = ByteUnits.decimal,
  3. String decimalSeparator = '.',
})

bytes in their unit under byteUnits (byteUnit), whole from 100 of it and to the tenth below (formatFigures). The sizes a DirectoryTree and a ProgressList write.

By default the units are the SI ones (ByteUnits.decimal), a kilobyte being 1000 bytes: 512 B, 4.1 kB, 620 kB, 1.2 MB, the kB of ls --si and du --si and of what disk makers and file managers quote. ByteUnits.binary counts by 1024 as ls -h and du -h do and names the units as the IEC does: 1023 B, 1.0 KiB, 1.5 MiB. A figure reads the same as the widgets' beside it where the same byteUnits are given to both.

final line = '${formatBytes(4096)} of ${formatBytes(1200000)}';
// line: 4.1 kB of 1.2 MB
final binary = formatBytes(1536, byteUnits: ByteUnits.binary);
// binary: 1.5 KiB

The point of 4.1 kB is decimalSeparator, TwkStrings.decimalSeparator for the widgets: 4,1 kB.

Implementation

String formatBytes(
  num bytes, {
  ByteUnits byteUnits = ByteUnits.decimal,
  String decimalSeparator = '.',
}) {
  final (scale, suffix) = byteUnit(bytes, byteUnits: byteUnits);
  return scale == 1 && bytes == bytes.roundToDouble()
      ? '${bytes.round()} B'
      : '${formatFigures(bytes / scale, decimalSeparator: decimalSeparator)} '
            '$suffix';
}