formatTableValue function

String formatTableValue(
  1. num? value,
  2. String unit
)

Compact display value+unit for a table cell: 41632 B -> '41 KB', 72 ms -> '72 ms', 4 -> '4'.

Implementation

String formatTableValue(num? value, String unit) {
  if (value == null) return 'n/a';
  final v = value.toDouble();
  if (unit == 'B') {
    if (v.abs() >= 1e6) return '${(v / 1e6).toStringAsFixed(1)} MB';
    if (v.abs() >= 1e3) return '${(v / 1e3).round()} KB';
    return '${v.round()} B';
  }
  if (unit == 'ms') return '${v.round()} ms';
  return '${v.round()}';
}