fastHumanizeSiBytes function

String fastHumanizeSiBytes(
  1. double bytes, {
  2. int precision = 2,
})

Ultra-fast formatter for simple SI bytes use-cases.

Constraints:

  • Standard: SI
  • Units: B, KB, MB, GB, TB, PB (bytes only)
  • No locale/grouping, no NBSP, no signed, no fullForm, no fixedWidth.
  • Precision: precision digits (trailing zeros trimmed).

This bypasses general features and takes the fastest route.

Implementation

String fastHumanizeSiBytes(double bytes, {int precision = 2}) {
  const tb = 1e12;
  const gb = 1e9;
  const mb = 1e6;
  const kb = 1e3;
  String sym;
  double base;
  if (bytes >= tb) {
    base = tb;
    sym = 'TB';
  } else if (bytes >= gb) {
    base = gb;
    sym = 'GB';
  } else if (bytes >= mb) {
    base = mb;
    sym = 'MB';
  } else if (bytes >= kb) {
    base = kb;
    sym = 'KB';
  } else {
    base = 1.0;
    sym = 'B';
  }
  final v = bytes / base;
  final s = _toFixedTrim(v, precision);
  return '$s $sym';
}