formatCompound function

String formatCompound(
  1. double bytes,
  2. CompoundFormatOptions opt
)

Formats a byte quantity (in bytes) to a compound mixed-unit string.

The opt.useBits flag toggles bit-based output (lowercase symbols). Unit labels can be localized via CompoundFormatOptions.fullForm and CompoundFormatOptions.fullForms. When CompoundFormatOptions.useGrouping is true, integer values >= 1000 will be grouped using the specified CompoundFormatOptions.locale (if provided) or the default locale.

Implementation

String formatCompound(double bytes, CompoundFormatOptions opt) {
  final quantity = opt.useBits ? (bytes * 8.0) : bytes;
  final parts = _decompose(quantity, opt);
  final locale = opt.locale;
  final fullForms = opt.fullForms;
  final numberFormat =
      opt.useGrouping ? NumberFormat.decimalPattern(locale) : null;
  final unitTexts = parts.map((p) {
    final value = p.$1;
    final sym = p.$2;
    final name = opt.fullForm
        ? _pluralize(sym, value, opt.useBits,
            locale: locale, overrides: fullForms)
        : sym;
    final numStr =
        numberFormat?.format(value.toInt()) ?? value.toStringAsFixed(0);
    return '$numStr${opt.spacer}$name';
  }).toList();
  return unitTexts.join(opt.separator);
}