Line data Source code
1 : extension NumberAbbreviation on num {
2 0 : String toStringAsAbbreviation({int precision = 2, bool uppercase = false}) {
3 : num abbreviationNumber = this;
4 : int count = 0;
5 0 : while (abbreviationNumber > 1000) {
6 0 : abbreviationNumber /= 1000;
7 0 : count += 1;
8 : }
9 : String suffix = switch (count) {
10 0 : 0 => "",
11 0 : 1 => "k",
12 0 : 2 => "m",
13 0 : 3 => "b",
14 0 : 4 => "t",
15 : _ => ""
16 : };
17 : if (uppercase) {
18 0 : suffix.toUpperCase();
19 : }
20 : return abbreviationNumber
21 0 : .toStringAsFixed(precision)
22 0 : .replaceFirst(RegExp(r'\.?0+$'), '') +
23 : suffix;
24 : }
25 : }
|