4 name
: 'Number formatting',
5 setup
: function(parent
) {
7 "<p>The default formatting mimicks printf with %.<i>p</i>g where <i>p</i> is" +
8 " the precision to use. It turns out that JavaScript's toPrecision()" +
9 " method is almost but not exactly equal to %g; they differ for values" +
10 " with small absolute values (10^-1 to 10^-5 or so), with toPrecision()" +
11 " yielding strings that are longer than they should be (i.e. using fixed" +
12 " point where %g would use exponential).</p>" +
14 "<p>This test is intended to check that our formatting works properly for a" +
15 " variety of precisions.</p>" +
17 "<p>Precision to use (1 to 21):" +
18 " <input type='text' id='p_input' size='20'></p>" +
20 "<div id='content' style='font-family:Courier New,monospace'></div>";
23 // Helper functions for generating an HTML table for holding the test
25 createRow
= function(columnType
, columns
) {
26 var row
= document
.createElement('tr');
27 for (var i
= 0; i
< columns
.length
; i
++) {
28 var th
= document
.createElement(columnType
);
29 var text
= document
.createTextNode(columns
[i
]);
36 createHeaderRow
= function(columns
) {
37 return createRow('th', columns
);
40 createDataRow
= function(columns
) {
41 return createRow('td', columns
);
44 createTable
= function(headerColumns
, dataColumnsList
) {
45 var table
= document
.createElement('table');
46 table
.appendChild(createHeaderRow(headerColumns
));
47 for (var i
= 0; i
< dataColumnsList
.length
; i
++) {
48 table
.appendChild(createDataRow(dataColumnsList
[i
]));
53 updateTable
= function() {
54 var headers
= ['Dygraph.floatFormat()', 'toPrecision()',
55 'Dygraph.floatFormat()', 'toPrecision()'];
57 var p
= parseInt(document
.getElementById('p_input').value
);
59 for (var i
= -10; i
<= 10; i
++) {
60 var n
= Math
.pow(10, i
);
61 numbers
.push([Dygraph
.floatFormat(n
, p
),
63 Dygraph
.floatFormat(Math
.PI
* n
, p
),
64 (Math
.PI
* n
).toPrecision(p
)]);
67 // Check exact values of 0.
68 numbers
.push([Dygraph
.floatFormat(0.0, p
),
71 var elem
= document
.getElementById('content');
73 elem
.appendChild(createTable(headers
, numbers
));
76 document
.getElementById('p_input').value
= '4';
77 document
.getElementById('p_input').onchange
= updateTable
;