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