Skip to content

Commit f88f7b0

Browse files
authored
Add number format function with 3 significant digits (#8135)
1 parent 820b5f3 commit f88f7b0

File tree

2 files changed

+63
-0
lines changed

2 files changed

+63
-0
lines changed

pkg/_pub_shared/lib/format/number_format.dart

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,29 @@ String formatWithSuffix(int value) {
1818
String _toFixed(int value, int d) {
1919
return (((value * 10) ~/ d) / 10).toStringAsFixed(1);
2020
}
21+
22+
/// Formats an int [value] to human readable chunk and suffix with at most 3
23+
/// significant digits.
24+
({String value, String suffix}) formatWith3SignificantDigits(int value) {
25+
if (value >= 999500000) {
26+
return (
27+
value: (value / 1000000000).toStringAsPrecision(3),
28+
suffix: 'B',
29+
);
30+
} else if (value >= 999500) {
31+
return (
32+
value: (value / 1000000).toStringAsPrecision(3),
33+
suffix: 'M',
34+
);
35+
} else if (value >= 1000) {
36+
return (
37+
value: (value / 1000).toStringAsPrecision(3),
38+
suffix: 'k',
39+
);
40+
} else {
41+
return (
42+
value: value.toString(),
43+
suffix: '',
44+
);
45+
}
46+
}

pkg/_pub_shared/test/format/number_format_test.dart

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,4 +29,41 @@ void main() {
2929
expect(formatWithSuffix(999999 + 1), '1.0m');
3030
expect(formatWithSuffix(1234000), '1.2m');
3131
});
32+
33+
test('Significant digit 0-999', () {
34+
expect(computeWith3SignificantDigits(0), (value: '0', suffix: ''));
35+
expect(computeWith3SignificantDigits(1), (value: '1', suffix: ''));
36+
expect(computeWith3SignificantDigits(23), (value: '23', suffix: ''));
37+
expect(computeWith3SignificantDigits(999), (value: '999', suffix: ''));
38+
});
39+
40+
test('Significant digit 1000-999499', () {
41+
expect(computeWith3SignificantDigits(1000), (value: '1.00', suffix: 'k'));
42+
expect(computeWith3SignificantDigits(1049), (value: '1.05', suffix: 'k'));
43+
expect(computeWith3SignificantDigits(1051), (value: '1.05', suffix: 'k'));
44+
expect(computeWith3SignificantDigits(1100), (value: '1.10', suffix: 'k'));
45+
expect(computeWith3SignificantDigits(9500), (value: '9.50', suffix: 'k'));
46+
expect(computeWith3SignificantDigits(99500), (value: '99.5', suffix: 'k'));
47+
expect(computeWith3SignificantDigits(100490), (value: '100', suffix: 'k'));
48+
expect(computeWith3SignificantDigits(100500), (value: '101', suffix: 'k'));
49+
expect(computeWith3SignificantDigits(199500), (value: '200', suffix: 'k'));
50+
expect(computeWith3SignificantDigits(999499), (value: '999', suffix: 'k'));
51+
});
52+
53+
test('Significant digit 999500-100000000', () {
54+
expect(computeWith3SignificantDigits(999500), (value: '1.00', suffix: 'M'));
55+
expect(computeWith3SignificantDigits(999999), (value: '1.00', suffix: 'M'));
56+
expect(
57+
computeWith3SignificantDigits(900000000), (value: '900', suffix: 'M'));
58+
expect(
59+
computeWith3SignificantDigits(999500000), (value: '1.00', suffix: 'B'));
60+
expect(computeWith3SignificantDigits(1009450000),
61+
(value: '1.01', suffix: 'B'));
62+
expect(computeWith3SignificantDigits(1094599999),
63+
(value: '1.09', suffix: 'B'));
64+
expect(computeWith3SignificantDigits(1095000001),
65+
(value: '1.10', suffix: 'B'));
66+
expect(computeWith3SignificantDigits(19000000000),
67+
(value: '19.0', suffix: 'B'));
68+
});
3269
}

0 commit comments

Comments
 (0)