Skip to content

Commit 1aa622f

Browse files
authored
Remove ending decimal zeros in number format method (#8343)
1 parent b7816e5 commit 1aa622f

File tree

4 files changed

+47
-41
lines changed

4 files changed

+47
-41
lines changed

app/lib/frontend/templates/views/pkg/labeled_scores.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,8 @@ d.Node labeledScoresNode({
3838
child: _labeledScore(
3939
'downloads',
4040
thirtyDaysDownloads != null
41-
? '${formatWith3SignificantDigits(thirtyDaysDownloads).value}'
42-
'${formatWith3SignificantDigits(thirtyDaysDownloads).suffix}'
41+
? '${compactFormat(thirtyDaysDownloads).value}'
42+
'${compactFormat(thirtyDaysDownloads).suffix}'
4343
: null,
4444
sign: '',
4545
),

app/lib/frontend/templates/views/pkg/score_tab.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -279,8 +279,8 @@ d.Node _downloadCountsKeyFigureNode(int? downloadCounts) {
279279
);
280280
}
281281
return _keyFigureNode(
282-
value: '${formatWith3SignificantDigits(downloadCounts).value}'
283-
'${formatWith3SignificantDigits(downloadCounts).suffix}',
282+
value: '${compactFormat(downloadCounts).value}'
283+
'${compactFormat(downloadCounts).suffix}',
284284
supplemental: '',
285285
label: 'downloads',
286286
);

pkg/_pub_shared/lib/format/number_format.dart

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,22 +33,34 @@ String formatWithThousandSeperators(int value) {
3333
return buffer.toString().split('').reversed.join();
3434
}
3535

36+
String removeDecimalZeros(String number) {
37+
final n = double.parse(number);
38+
39+
if (n - n.truncate() == 0) {
40+
return n.truncate().toString();
41+
}
42+
if (n * 10 - (n * 10).truncate() == 0) {
43+
return (((n * 10).truncate()) / 10).toString();
44+
}
45+
return number;
46+
}
47+
3648
/// Formats an int [value] to human readable chunk and suffix with at most 3
37-
/// significant digits.
38-
({String value, String suffix}) formatWith3SignificantDigits(int value) {
49+
/// significant digits. E.g. 1200000 becomes 1.2 M.
50+
({String value, String suffix}) compactFormat(int value) {
3951
if (value >= 999500000) {
4052
return (
41-
value: (value / 1000000000).toStringAsPrecision(3),
53+
value: removeDecimalZeros((value / 1000000000).toStringAsPrecision(3)),
4254
suffix: 'B',
4355
);
4456
} else if (value >= 999500) {
4557
return (
46-
value: (value / 1000000).toStringAsPrecision(3),
58+
value: removeDecimalZeros((value / 1000000).toStringAsPrecision(3)),
4759
suffix: 'M',
4860
);
4961
} else if (value >= 1000) {
5062
return (
51-
value: (value / 1000).toStringAsPrecision(3),
63+
value: removeDecimalZeros((value / 1000).toStringAsPrecision(3)),
5264
suffix: 'k',
5365
);
5466
} else {

pkg/_pub_shared/test/format/number_format_test.dart

Lines changed: 26 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -30,45 +30,39 @@ void main() {
3030
expect(formatWithSuffix(1234000), '1.2m');
3131
});
3232

33-
test('Significant digit 0-999', () {
34-
expect(formatWith3SignificantDigits(0), (value: '0', suffix: ''));
35-
expect(formatWith3SignificantDigits(1), (value: '1', suffix: ''));
36-
expect(formatWith3SignificantDigits(23), (value: '23', suffix: ''));
37-
expect(formatWith3SignificantDigits(999), (value: '999', suffix: ''));
33+
test('Compact format 0-999', () {
34+
expect(compactFormat(0), (value: '0', suffix: ''));
35+
expect(compactFormat(1), (value: '1', suffix: ''));
36+
expect(compactFormat(23), (value: '23', suffix: ''));
37+
expect(compactFormat(999), (value: '999', suffix: ''));
3838
});
3939

40-
test('Significant digit 1000-999499', () {
41-
expect(formatWith3SignificantDigits(1000), (value: '1.00', suffix: 'k'));
42-
expect(formatWith3SignificantDigits(1049), (value: '1.05', suffix: 'k'));
43-
expect(formatWith3SignificantDigits(1051), (value: '1.05', suffix: 'k'));
44-
expect(formatWith3SignificantDigits(1100), (value: '1.10', suffix: 'k'));
45-
expect(formatWith3SignificantDigits(9500), (value: '9.50', suffix: 'k'));
46-
expect(formatWith3SignificantDigits(99500), (value: '99.5', suffix: 'k'));
47-
expect(formatWith3SignificantDigits(100490), (value: '100', suffix: 'k'));
48-
expect(formatWith3SignificantDigits(100500), (value: '101', suffix: 'k'));
49-
expect(formatWith3SignificantDigits(199500), (value: '200', suffix: 'k'));
50-
expect(formatWith3SignificantDigits(999499), (value: '999', suffix: 'k'));
40+
test('Compact format 1000-999499', () {
41+
expect(compactFormat(1000), (value: '1', suffix: 'k'));
42+
expect(compactFormat(1049), (value: '1.05', suffix: 'k'));
43+
expect(compactFormat(1051), (value: '1.05', suffix: 'k'));
44+
expect(compactFormat(1100), (value: '1.1', suffix: 'k'));
45+
expect(compactFormat(9500), (value: '9.5', suffix: 'k'));
46+
expect(compactFormat(99500), (value: '99.5', suffix: 'k'));
47+
expect(compactFormat(100490), (value: '100', suffix: 'k'));
48+
expect(compactFormat(100500), (value: '101', suffix: 'k'));
49+
expect(compactFormat(199500), (value: '200', suffix: 'k'));
50+
expect(compactFormat(999499), (value: '999', suffix: 'k'));
5151
});
5252

53-
test('Significant digit 999500-100000000', () {
54-
expect(formatWith3SignificantDigits(999500), (value: '1.00', suffix: 'M'));
55-
expect(formatWith3SignificantDigits(999999), (value: '1.00', suffix: 'M'));
56-
expect(
57-
formatWith3SignificantDigits(900000000), (value: '900', suffix: 'M'));
58-
expect(
59-
formatWith3SignificantDigits(999500000), (value: '1.00', suffix: 'B'));
60-
expect(
61-
formatWith3SignificantDigits(1009450000), (value: '1.01', suffix: 'B'));
62-
expect(
63-
formatWith3SignificantDigits(1094599999), (value: '1.09', suffix: 'B'));
64-
expect(
65-
formatWith3SignificantDigits(1095000001), (value: '1.10', suffix: 'B'));
66-
expect(formatWith3SignificantDigits(19000000000),
67-
(value: '19.0', suffix: 'B'));
53+
test('Compact format 999500-100000000', () {
54+
expect(compactFormat(999500), (value: '1', suffix: 'M'));
55+
expect(compactFormat(999999), (value: '1', suffix: 'M'));
56+
expect(compactFormat(900000000), (value: '900', suffix: 'M'));
57+
expect(compactFormat(999500000), (value: '1', suffix: 'B'));
58+
expect(compactFormat(1009450000), (value: '1.01', suffix: 'B'));
59+
expect(compactFormat(1094599999), (value: '1.09', suffix: 'B'));
60+
expect(compactFormat(1095000001), (value: '1.1', suffix: 'B'));
61+
expect(compactFormat(19000000000), (value: '19', suffix: 'B'));
6862
});
6963

7064
test('Number with thousand seperators', () {
71-
// expect(formatWithThousandSeperators(1), '1');
65+
expect(formatWithThousandSeperators(1), '1');
7266
expect(formatWithThousandSeperators(10), '10');
7367
expect(formatWithThousandSeperators(100), '100');
7468
expect(formatWithThousandSeperators(1000), '1,000');

0 commit comments

Comments
 (0)