toStringAsAbbreviation method

String toStringAsAbbreviation(
  1. {int precision = 2,
  2. bool uppercase = false}
)

Implementation

String toStringAsAbbreviation({int precision = 2, bool uppercase = false}) {
  num abbreviationNumber = this;
  int count = 0;
  while (abbreviationNumber >= 1000 && count < 4) {
    abbreviationNumber /= 1000;
    count += 1;
  }
  String suffix = switch (count) {
    0 => "",
    1 => "k",
    2 => "m",
    3 => "b",
    4 => "t",
    _ => ""
  };
  if (uppercase) {
    suffix = suffix.toUpperCase();
  }
  return abbreviationNumber
          .toStringAsFixed(precision)
          .replaceFirst(RegExp(r'\.?0+$'), '') +
      suffix;
}