Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions app/lib/frontend/templates/views/pkg/labeled_scores.dart
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ d.Node labeledScoresNode({
child: _labeledScore(
'downloads',
thirtyDaysDownloads != null
? '${formatWith3SignificantDigits(thirtyDaysDownloads).value}'
'${formatWith3SignificantDigits(thirtyDaysDownloads).suffix}'
? '${compactFormat(thirtyDaysDownloads).value}'
'${compactFormat(thirtyDaysDownloads).suffix}'
: null,
sign: '',
),
Expand Down
4 changes: 2 additions & 2 deletions app/lib/frontend/templates/views/pkg/score_tab.dart
Original file line number Diff line number Diff line change
Expand Up @@ -279,8 +279,8 @@ d.Node _downloadCountsKeyFigureNode(int? downloadCounts) {
);
}
return _keyFigureNode(
value: '${formatWith3SignificantDigits(downloadCounts).value}'
'${formatWith3SignificantDigits(downloadCounts).suffix}',
value: '${compactFormat(downloadCounts).value}'
'${compactFormat(downloadCounts).suffix}',
supplemental: '',
label: 'downloads',
);
Expand Down
22 changes: 17 additions & 5 deletions pkg/_pub_shared/lib/format/number_format.dart
Original file line number Diff line number Diff line change
Expand Up @@ -33,22 +33,34 @@ String formatWithThousandSeperators(int value) {
return buffer.toString().split('').reversed.join();
}

String removeDecimalZeros(String number) {
final n = double.parse(number);

if (n - n.truncate() == 0) {
return n.truncate().toString();
}
if (n * 10 - (n * 10).truncate() == 0) {
return (((n * 10).truncate()) / 10).toString();
}
return number;
}

/// Formats an int [value] to human readable chunk and suffix with at most 3
/// significant digits.
({String value, String suffix}) formatWith3SignificantDigits(int value) {
/// significant digits. E.g. 1200000 becomes 1.2 M.
({String value, String suffix}) compactFormat(int value) {
if (value >= 999500000) {
return (
value: (value / 1000000000).toStringAsPrecision(3),
value: removeDecimalZeros((value / 1000000000).toStringAsPrecision(3)),
suffix: 'B',
);
} else if (value >= 999500) {
return (
value: (value / 1000000).toStringAsPrecision(3),
value: removeDecimalZeros((value / 1000000).toStringAsPrecision(3)),
suffix: 'M',
);
} else if (value >= 1000) {
return (
value: (value / 1000).toStringAsPrecision(3),
value: removeDecimalZeros((value / 1000).toStringAsPrecision(3)),
suffix: 'k',
);
} else {
Expand Down
58 changes: 26 additions & 32 deletions pkg/_pub_shared/test/format/number_format_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -30,45 +30,39 @@ void main() {
expect(formatWithSuffix(1234000), '1.2m');
});

test('Significant digit 0-999', () {
expect(formatWith3SignificantDigits(0), (value: '0', suffix: ''));
expect(formatWith3SignificantDigits(1), (value: '1', suffix: ''));
expect(formatWith3SignificantDigits(23), (value: '23', suffix: ''));
expect(formatWith3SignificantDigits(999), (value: '999', suffix: ''));
test('Compact format 0-999', () {
expect(compactFormat(0), (value: '0', suffix: ''));
expect(compactFormat(1), (value: '1', suffix: ''));
expect(compactFormat(23), (value: '23', suffix: ''));
expect(compactFormat(999), (value: '999', suffix: ''));
});

test('Significant digit 1000-999499', () {
expect(formatWith3SignificantDigits(1000), (value: '1.00', suffix: 'k'));
expect(formatWith3SignificantDigits(1049), (value: '1.05', suffix: 'k'));
expect(formatWith3SignificantDigits(1051), (value: '1.05', suffix: 'k'));
expect(formatWith3SignificantDigits(1100), (value: '1.10', suffix: 'k'));
expect(formatWith3SignificantDigits(9500), (value: '9.50', suffix: 'k'));
expect(formatWith3SignificantDigits(99500), (value: '99.5', suffix: 'k'));
expect(formatWith3SignificantDigits(100490), (value: '100', suffix: 'k'));
expect(formatWith3SignificantDigits(100500), (value: '101', suffix: 'k'));
expect(formatWith3SignificantDigits(199500), (value: '200', suffix: 'k'));
expect(formatWith3SignificantDigits(999499), (value: '999', suffix: 'k'));
test('Compact format 1000-999499', () {
expect(compactFormat(1000), (value: '1', suffix: 'k'));
expect(compactFormat(1049), (value: '1.05', suffix: 'k'));
expect(compactFormat(1051), (value: '1.05', suffix: 'k'));
expect(compactFormat(1100), (value: '1.1', suffix: 'k'));
expect(compactFormat(9500), (value: '9.5', suffix: 'k'));
expect(compactFormat(99500), (value: '99.5', suffix: 'k'));
expect(compactFormat(100490), (value: '100', suffix: 'k'));
expect(compactFormat(100500), (value: '101', suffix: 'k'));
expect(compactFormat(199500), (value: '200', suffix: 'k'));
expect(compactFormat(999499), (value: '999', suffix: 'k'));
});

test('Significant digit 999500-100000000', () {
expect(formatWith3SignificantDigits(999500), (value: '1.00', suffix: 'M'));
expect(formatWith3SignificantDigits(999999), (value: '1.00', suffix: 'M'));
expect(
formatWith3SignificantDigits(900000000), (value: '900', suffix: 'M'));
expect(
formatWith3SignificantDigits(999500000), (value: '1.00', suffix: 'B'));
expect(
formatWith3SignificantDigits(1009450000), (value: '1.01', suffix: 'B'));
expect(
formatWith3SignificantDigits(1094599999), (value: '1.09', suffix: 'B'));
expect(
formatWith3SignificantDigits(1095000001), (value: '1.10', suffix: 'B'));
expect(formatWith3SignificantDigits(19000000000),
(value: '19.0', suffix: 'B'));
test('Compact format 999500-100000000', () {
expect(compactFormat(999500), (value: '1', suffix: 'M'));
expect(compactFormat(999999), (value: '1', suffix: 'M'));
expect(compactFormat(900000000), (value: '900', suffix: 'M'));
expect(compactFormat(999500000), (value: '1', suffix: 'B'));
expect(compactFormat(1009450000), (value: '1.01', suffix: 'B'));
expect(compactFormat(1094599999), (value: '1.09', suffix: 'B'));
expect(compactFormat(1095000001), (value: '1.1', suffix: 'B'));
expect(compactFormat(19000000000), (value: '19', suffix: 'B'));
});

test('Number with thousand seperators', () {
// expect(formatWithThousandSeperators(1), '1');
expect(formatWithThousandSeperators(1), '1');
expect(formatWithThousandSeperators(10), '10');
expect(formatWithThousandSeperators(100), '100');
expect(formatWithThousandSeperators(1000), '1,000');
Expand Down