Skip to content

Commit c375571

Browse files
committed
Use custom format method
1 parent 50c0bf6 commit c375571

File tree

3 files changed

+29
-1
lines changed

3 files changed

+29
-1
lines changed

pkg/_pub_shared/lib/format/number_format.dart

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,19 @@ String formatWithSuffix(int value) {
1515
}
1616
}
1717

18+
String formatWithThousandSeperators(int value) {
19+
final reversed = value.toString().split('').reversed.toList();
20+
final buffer = StringBuffer();
21+
for (int j = 0; j < reversed.length; j++) {
22+
if (j > 0 && j % 3 == 0) {
23+
buffer.write(',');
24+
}
25+
buffer.write(reversed[j]);
26+
}
27+
28+
return buffer.toString().split('').reversed.join();
29+
}
30+
1831
String _toFixed(int value, int d) {
1932
return (((value * 10) ~/ d) / 10).toStringAsFixed(1);
2033
}

pkg/_pub_shared/test/format/number_format_test.dart

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,4 +66,18 @@ void main() {
6666
expect(formatWith3SignificantDigits(19000000000),
6767
(value: '19.0', suffix: 'B'));
6868
});
69+
70+
test('Number with thousand seperators', () {
71+
// expect(formatWithThousandSeperators(1), '1');
72+
expect(formatWithThousandSeperators(10), '10');
73+
expect(formatWithThousandSeperators(100), '100');
74+
expect(formatWithThousandSeperators(1000), '1,000');
75+
expect(formatWithThousandSeperators(10000), '10,000');
76+
expect(formatWithThousandSeperators(100000), '100,000');
77+
expect(formatWithThousandSeperators(1000000), '1,000,000');
78+
expect(formatWithThousandSeperators(10000000), '10,000,000');
79+
expect(formatWithThousandSeperators(100000000), '100,000,000');
80+
expect(formatWithThousandSeperators(1000000000), '1,000,000,000');
81+
expect(formatWithThousandSeperators(10000000000), '10,000,000,000');
82+
});
6983
}

pkg/web_app/lib/src/widget/weekly_sparkline/widget.dart

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import 'dart:math';
22

33
import 'package:_pub_shared/format/encoding.dart';
4+
import 'package:_pub_shared/format/number_format.dart';
45
import 'package:intl/intl.dart';
56
import 'package:web/web.dart';
67

@@ -146,7 +147,7 @@ void drawChart(Element svg, HTMLDivElement toolTip, HTMLDivElement chartSubText,
146147
final coords = computeCoordinates(selectedDay.date, selectedDay.downloads);
147148
sparklineSpot.setAttribute('cy', '${coords.$2}');
148149
sparklineCursor.setAttribute('transform', 'translate(${coords.$1}, 0)');
149-
toolTip.text = '${NumberFormat('#,###').format(selectedDay.downloads)}';
150+
toolTip.text = '${formatWithThousandSeperators(selectedDay.downloads)}';
150151

151152
final startDate = selectedDay.date.subtract(Duration(days: 7));
152153
chartSubText.text =

0 commit comments

Comments
 (0)