toKMB method

String toKMB()

Short count. 1500 → "1.5 K", 2300000 → "2.3 M"

Implementation

String toKMB() {
  if (this < 1000) return toString();
  if (this < 100000) return "${(this / 1000).toStringAsFixed(1)} K";
  if (this < 1000000) return "${(this / 1000).toStringAsFixed(0)} K";
  if (this < 1000000000) return "${(this / 1000000).toStringAsFixed(1)} M";
  return "${(this / 1000000000).toStringAsFixed(1)} B";
}