We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 0e70bf0 commit cf69358Copy full SHA for cf69358
pkg/_pub_shared/lib/format/number_format.dart
@@ -19,6 +19,20 @@ String _toFixed(int value, int d) {
19
return (((value * 10) ~/ d) / 10).toStringAsFixed(1);
20
}
21
22
+/// Formats an int [value] with commas as thousand seperators.
23
+String formatWithThousandSeperators(int value) {
24
+ final digits = value.toString().split('');
25
+ final l = digits.length - 1;
26
+ final buffer = StringBuffer();
27
+ for (int j = 0; j <= l; j++) {
28
+ if (j > 0 && j % 3 == 0) {
29
+ buffer.write(',');
30
+ }
31
+ buffer.write(digits[l - j]);
32
33
+ return buffer.toString().split('').reversed.join();
34
+}
35
+
36
/// 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) {
pkg/_pub_shared/test/format/number_format_test.dart
@@ -66,4 +66,18 @@ void main() {
66
expect(formatWith3SignificantDigits(19000000000),
67
(value: '19.0', suffix: 'B'));
68
});
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
+ });
83
pkg/web_app/lib/src/widget/weekly_sparkline/widget.dart
@@ -1,6 +1,7 @@
1
import 'dart:math';
2
3
import 'package:_pub_shared/format/encoding.dart';
4
+import 'package:_pub_shared/format/number_format.dart';
5
import 'package:web/web.dart';
6
7
void create(HTMLElement element, Map<String, String> options) {
@@ -145,8 +146,7 @@ void drawChart(Element svg, HTMLDivElement toolTip, HTMLDivElement chartSubText,
145
146
final coords = computeCoordinates(selectedDay.date, selectedDay.downloads);
147
sparklineSpot.setAttribute('cy', '${coords.$2}');
148
sparklineCursor.setAttribute('transform', 'translate(${coords.$1}, 0)');
-
149
- toolTip.text = '${selectedDay.downloads}';
+ toolTip.text = '${formatWithThousandSeperators(selectedDay.downloads)}';
150
151
final startDate = selectedDay.date.subtract(Duration(days: 7));
152
chartSubText.text =
0 commit comments