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>
9 <script type=
"text/javascript" src=
"../strftime/strftime-min.js"></script>
10 <script type=
"text/javascript" src=
"../rgbcolor/rgbcolor.js"></script>
11 <script type=
"text/javascript" src=
"../dygraph-canvas.js"></script>
12 <script type=
"text/javascript" src=
"../dygraph.js"></script>
15 <p>The default formatting mimicks printf with %.
<i>p
</i>g where
<i>p
</i> is
16 the precision to use. It turns out that JavaScript's toPrecision()
17 method is almost but not exactly equal to %g; they differ for values
18 with small absolute values (
10^-
1 to
10^-
5 or so), with toPrecision()
19 yielding strings that are longer than they should be (i.e. using fixed
20 point where %g would use exponential).
</p>
22 <p>This test is intended to check that our formatting works properly for a
23 variety of precisions.
</p>
25 <p>Precision to use (
1 to
21):
26 <input type=
"text" id=
"p_input" size=
"20" onchange=
"updateTable();"></p>
29 <div id=
"content" style=
"font-family:'Courier New',monospace"></div>
31 <script type=
"text/javascript">
32 // Helper functions for generating an HTML table for holding the test
34 createRow = function(columnType, columns) {
35 var row = document.createElement('tr');
36 for (var i =
0; i < columns.length; i ++) {
37 var th = document.createElement(columnType);
38 var text = document.createTextNode(columns[i]);
45 createHeaderRow = function(columns) {
46 return createRow('th', columns);
49 createDataRow = function(columns) {
50 return createRow('td', columns);
53 createTable = function(headerColumns, dataColumnsList) {
54 var table = document.createElement('table');
55 table.appendChild(createHeaderRow(headerColumns));
56 for (var i =
0; i < dataColumnsList.length; i++) {
57 table.appendChild(createDataRow(dataColumnsList[i]));
62 updateTable = function() {
63 var headers = ['Dygraph.floatFormat()', 'toPrecision()',
64 'Dygraph.floatFormat()', 'toPrecision()'];
66 var p = parseInt(document.getElementById('p_input').value);
68 for (var i = -
10; i <=
10; i++) {
69 var n = Math.pow(
10, i);
70 numbers.push([Dygraph.floatFormat(n, p),
72 Dygraph.floatFormat(Math.PI * n, p),
73 (Math.PI * n).toPrecision(p)]);
76 // Check exact values of
0.
77 numbers.push([Dygraph.floatFormat(
0.0, p),
80 var elem = document.getElementById('content');
82 elem.appendChild(createTable(headers, numbers));
85 document.getElementById('p_input').value = '
4';