4 <title>Test of number formatting
</title>
6 For production (minified) code, use:
7 <script type=
"text/javascript" src=
"dygraph-combined.js"></script>
9 <script type=
"text/javascript" src=
"../dygraph-dev.js"></script>
13 <p>The default formatting mimicks printf with %.
<i>p
</i>g where
<i>p
</i> is
14 the precision to use. It turns out that JavaScript's toPrecision()
15 method is almost but not exactly equal to %g; they differ for values
16 with small absolute values (
10^-
1 to
10^-
5 or so), with toPrecision()
17 yielding strings that are longer than they should be (i.e. using fixed
18 point where %g would use exponential).
</p>
20 <p>This test is intended to check that our formatting works properly for a
21 variety of precisions.
</p>
23 <p>Precision to use (
1 to
21):
24 <input type=
"text" id=
"p_input" size=
"20" onchange=
"updateTable();"></p>
27 <div id=
"content" style=
"font-family:'Courier New',monospace"></div>
29 <script type=
"text/javascript">
30 // Helper functions for generating an HTML table for holding the test
32 createRow = function(columnType, columns) {
33 var row = document.createElement('tr');
34 for (var i =
0; i < columns.length; i ++) {
35 var th = document.createElement(columnType);
36 var text = document.createTextNode(columns[i]);
43 createHeaderRow = function(columns) {
44 return createRow('th', columns);
47 createDataRow = function(columns) {
48 return createRow('td', columns);
51 createTable = function(headerColumns, dataColumnsList) {
52 var table = document.createElement('table');
53 table.appendChild(createHeaderRow(headerColumns));
54 for (var i =
0; i < dataColumnsList.length; i++) {
55 table.appendChild(createDataRow(dataColumnsList[i]));
60 updateTable = function() {
61 var headers = ['Dygraph.floatFormat()', 'toPrecision()',
62 'Dygraph.floatFormat()', 'toPrecision()'];
64 var p = parseInt(document.getElementById('p_input').value);
66 for (var i = -
10; i <=
10; i++) {
67 var n = Math.pow(
10, i);
68 numbers.push([Dygraph.floatFormat(n, p),
70 Dygraph.floatFormat(Math.PI * n, p),
71 (Math.PI * n).toPrecision(p)]);
74 // Check exact values of
0.
75 numbers.push([Dygraph.floatFormat(
0.0, p),
78 var elem = document.getElementById('content');
80 elem.appendChild(createTable(headers, numbers));
83 document.getElementById('p_input').value = '
4';