forked from d3/d3-format
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathformat-type-s-test.js
159 lines (151 loc) · 5.38 KB
/
format-type-s-test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
var tape = require("tape"),
format = require("../");
tape("format(\"s\") outputs SI-prefix notation with default precision 6", function(test) {
var f = format.format("s");
test.equal(f(0), "0.00000");
test.equal(f(1), "1.00000");
test.equal(f(10), "10.0000");
test.equal(f(100), "100.000");
test.equal(f(999.5), "999.500");
test.equal(f(999500), "999.500k");
test.equal(f(1000), "1.00000k");
test.equal(f(100), "100.000");
test.equal(f(1400), "1.40000k");
test.equal(f(1500.5), "1.50050k");
test.equal(f(.00001), "10.0000µ");
test.equal(f(.000001), "1.00000µ");
test.end();
});
tape("format(\"[.precision]s\") outputs SI-prefix notation with precision significant digits", function(test) {
var f = format.format(".3s");
test.equal(f(0), "0.00");
test.equal(f(1), "1.00");
test.equal(f(10), "10.0");
test.equal(f(100), "100");
test.equal(f(999.5), "1.00k");
test.equal(f(999500), "1.00M");
test.equal(f(1000), "1.00k");
test.equal(f(1500.5), "1.50k");
test.equal(f(145500000), "146M");
test.equal(f(145999999.999999347), "146M");
test.equal(f(1e26), "100Y");
test.equal(f(.000001), "1.00µ");
test.equal(f(.009995), "10.0m");
var f = format.format(".4s");
test.equal(f(999.5), "999.5");
test.equal(f(999500), "999.5k");
test.equal(f(.009995), "9.995m");
test.end();
});
tape("format(\"s\") formats numbers smaller than 1e-24 with yocto", function(test) {
var f = format.format(".8s");
test.equal(f(1.29e-30), "0.0000013y"); // Note: rounded!
test.equal(f(1.29e-29), "0.0000129y");
test.equal(f(1.29e-28), "0.0001290y");
test.equal(f(1.29e-27), "0.0012900y");
test.equal(f(1.29e-26), "0.0129000y");
test.equal(f(1.29e-25), "0.1290000y");
test.equal(f(1.29e-24), "1.2900000y");
test.equal(f(1.29e-23), "12.900000y");
test.equal(f(1.29e-22), "129.00000y");
test.equal(f(1.29e-21), "1.2900000z");
test.equal(f(-1.29e-30), "-0.0000013y"); // Note: rounded!
test.equal(f(-1.29e-29), "-0.0000129y");
test.equal(f(-1.29e-28), "-0.0001290y");
test.equal(f(-1.29e-27), "-0.0012900y");
test.equal(f(-1.29e-26), "-0.0129000y");
test.equal(f(-1.29e-25), "-0.1290000y");
test.equal(f(-1.29e-24), "-1.2900000y");
test.equal(f(-1.29e-23), "-12.900000y");
test.equal(f(-1.29e-22), "-129.00000y");
test.equal(f(-1.29e-21), "-1.2900000z");
test.end();
});
tape("format(\"s\") formats numbers larger than 1e24 with yotta", function(test) {
var f = format.format(".8s");
test.equal(f(1.23e+21), "1.2300000Z");
test.equal(f(1.23e+22), "12.300000Z");
test.equal(f(1.23e+23), "123.00000Z");
test.equal(f(1.23e+24), "1.2300000Y");
test.equal(f(1.23e+25), "12.300000Y");
test.equal(f(1.23e+26), "123.00000Y");
test.equal(f(1.23e+27), "1230.0000Y");
test.equal(f(1.23e+28), "12300.000Y");
test.equal(f(1.23e+29), "123000.00Y");
test.equal(f(1.23e+30), "1230000.0Y");
test.equal(f(-1.23e+21), "-1.2300000Z");
test.equal(f(-1.23e+22), "-12.300000Z");
test.equal(f(-1.23e+23), "-123.00000Z");
test.equal(f(-1.23e+24), "-1.2300000Y");
test.equal(f(-1.23e+25), "-12.300000Y");
test.equal(f(-1.23e+26), "-123.00000Y");
test.equal(f(-1.23e+27), "-1230.0000Y");
test.equal(f(-1.23e+28), "-12300.000Y");
test.equal(f(-1.23e+29), "-123000.00Y");
test.equal(f(-1.23e+30), "-1230000.0Y");
test.end();
});
tape("format(\"$s\") outputs SI-prefix notation with a currency symbol", function(test) {
var f = format.format("$.2s");
test.equal(f(0), "$0.0");
test.equal(f(2.5e5), "$250k");
test.equal(f(-2.5e8), "-$250M");
test.equal(f(2.5e11), "$250G");
var f = format.format("$.3s");
test.equal(f(0), "$0.00");
test.equal(f(1), "$1.00");
test.equal(f(10), "$10.0");
test.equal(f(100), "$100");
test.equal(f(999.5), "$1.00k");
test.equal(f(999500), "$1.00M");
test.equal(f(1000), "$1.00k");
test.equal(f(1500.5), "$1.50k");
test.equal(f(145500000), "$146M");
test.equal(f(145999999.999999347), "$146M");
test.equal(f(1e26), "$100Y");
test.equal(f(.000001), "$1.00µ");
test.equal(f(.009995), "$10.0m");
var f = format.format("$.4s");
test.equal(f(999.5), "$999.5");
test.equal(f(999500), "$999.5k");
test.equal(f(.009995), "$9.995m");
test.end();
});
tape("format(\"s\") SI-prefix notation precision is consistent for small and large numbers", function(test) {
var f = format.format(".0s");
test.equal(f(1e-5), "10µ");
test.equal(f(1e-4), "100µ");
test.equal(f(1e-3), "1m");
test.equal(f(1e-2), "10m");
test.equal(f(1e-1), "100m");
test.equal(f(1e+0), "1");
test.equal(f(1e+1), "10");
test.equal(f(1e+2), "100");
test.equal(f(1e+3), "1k");
test.equal(f(1e+4), "10k");
test.equal(f(1e+5), "100k");
var f = format.format(".4s");
test.equal(f(1e-5), "10.00µ");
test.equal(f(1e-4), "100.0µ");
test.equal(f(1e-3), "1.000m");
test.equal(f(1e-2), "10.00m");
test.equal(f(1e-1), "100.0m");
test.equal(f(1e+0), "1.000");
test.equal(f(1e+1), "10.00");
test.equal(f(1e+2), "100.0");
test.equal(f(1e+3), "1.000k");
test.equal(f(1e+4), "10.00k");
test.equal(f(1e+5), "100.0k");
test.end();
});
tape("format(\"0[width],s\") will group thousands due to zero fill", function(test) {
var f = format.format("020,s");
test.equal(f(42), "000,000,000,042.0000");
test.equal(f(42e12), "00,000,000,042.0000T");
test.end();
});
tape("format(\",s\") will group thousands for very large numbers", function(test) {
var f = format.format(",s");
test.equal(f(42e30), "42,000,000Y");
test.end();
});