4 <meta http-equiv=
"X-UA-Compatible" content=
"IE=EmulateIE7; IE=EmulateIE9">
5 <title>Test of number formatting
</title>
7 <script type=
"text/javascript" src=
"../excanvas.js"></script>
10 For production (minified) code, use:
11 <script type=
"text/javascript" src=
"dygraph-combined.js"></script>
13 <script type=
"text/javascript" src=
"../dygraph-dev.js"></script>
17 <p>The default formatting mimicks printf with %.
<i>p
</i>g where
<i>p
</i> is
18 the precision to use. It turns out that JavaScript's toPrecision()
19 method is almost but not exactly equal to %g; they differ for values
20 with small absolute values (
10^-
1 to
10^-
5 or so), with toPrecision()
21 yielding strings that are longer than they should be (i.e. using fixed
22 point where %g would use exponential).
</p>
24 <p>This test is intended to check that our formatting works properly for a
25 variety of precisions.
</p>
27 <p>Precision to use (
1 to
21):
28 <input type=
"text" id=
"p_input" size=
"20" onchange=
"updateTable();"></p>
31 <div id=
"content" style=
"font-family:'Courier New',monospace"></div>
33 <script type=
"text/javascript">
34 // Helper functions for generating an HTML table for holding the test
36 createRow = function(columnType, columns) {
37 var row = document.createElement('tr');
38 for (var i =
0; i < columns.length; i ++) {
39 var th = document.createElement(columnType);
40 var text = document.createTextNode(columns[i]);
47 createHeaderRow = function(columns) {
48 return createRow('th', columns);
51 createDataRow = function(columns) {
52 return createRow('td', columns);
55 createTable = function(headerColumns, dataColumnsList) {
56 var table = document.createElement('table');
57 table.appendChild(createHeaderRow(headerColumns));
58 for (var i =
0; i < dataColumnsList.length; i++) {
59 table.appendChild(createDataRow(dataColumnsList[i]));
64 updateTable = function() {
65 var headers = ['Dygraph.floatFormat()', 'toPrecision()',
66 'Dygraph.floatFormat()', 'toPrecision()'];
68 var p = parseInt(document.getElementById('p_input').value);
70 for (var i = -
10; i <=
10; i++) {
71 var n = Math.pow(
10, i);
72 numbers.push([Dygraph.floatFormat(n, p),
74 Dygraph.floatFormat(Math.PI * n, p),
75 (Math.PI * n).toPrecision(p)]);
78 // Check exact values of
0.
79 numbers.push([Dygraph.floatFormat(
0.0, p),
82 var elem = document.getElementById('content');
84 elem.appendChild(createTable(headers, numbers));
87 document.getElementById('p_input').value = '
4';