File: number-format.html

package info (click to toggle)
dygraphs 2.2.1-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 4,340 kB
  • sloc: javascript: 24,842; sh: 800; python: 581; makefile: 45
file content (88 lines) | stat: -rw-r--r-- 3,161 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
<!DOCTYPE html>
<html>
  <head>
    <meta charset="UTF-8">
    <title>Test of number formatting</title>

    <link rel="stylesheet" type="text/css" href="../dist/dygraph.css" />
    <link rel="stylesheet" type="text/css" href="../common/vextlnk.css" />
    <script type="text/javascript" src="../dist/dygraph.js"></script>
  </head>
  <body>
    <p>The default formatting mimics printf with %.<i>p</i>g where <i>p</i> is
       the precision to use.  It turns out that JavaScript's toPrecision()
       method is almost but not exactly equal to %g; they differ for values
       with small absolute values (10^-1 to 10^-5 or so), with toPrecision()
       yielding strings that are longer than they should be (i.e. using fixed
       point where %g would use exponential).</p>

    <p>This test is intended to check that our formatting works properly for a
       variety of precisions.</p>

    <p>Precision to use (1 to 21):
      <input type="text" id="p_input" size="20" onchange="updateTable();"></p>
    <br>
    <br>
    <div id="content" style="font-family:'Courier New',monospace"></div>

    <script type="text/javascript"><!--//--><![CDATA[//><!--
    Dygraph.onDOMready(function onDOMready() {
      // Helper functions for generating an HTML table for holding the test
      // results.
      createRow = function(columnType, columns) {
        var row = document.createElement('tr');
        for (var i = 0; i  < columns.length; i ++) {
          var th = document.createElement(columnType);
          var text = document.createTextNode(columns[i]);
          th.appendChild(text);
          row.appendChild(th);
        };
        return row;
      };

      createHeaderRow = function(columns) {
        return createRow('th', columns);
      };

      createDataRow = function(columns) {
        return createRow('td', columns);
      };

      createTable = function(headerColumns, dataColumnsList) {
        var table = document.createElement('table');
        table.appendChild(createHeaderRow(headerColumns));
        for (var i = 0; i < dataColumnsList.length; i++) {
          table.appendChild(createDataRow(dataColumnsList[i]));
        }
        return table;
      };

      updateTable = function() {
        var headers = ['Dygraph.floatFormat()', 'toPrecision()',
                       'Dygraph.floatFormat()', 'toPrecision()'];
        var numbers = [];
        var p = parseInt(document.getElementById('p_input').value);

        for (var i = -10; i <= 10; i++) {
          var n = Math.pow(10, i);
          numbers.push([Dygraph.floatFormat(n, p),
                        n.toPrecision(p),
                        Dygraph.floatFormat(Math.PI * n, p),
                        (Math.PI * n).toPrecision(p)]);
        }

        // Check exact values of 0.
        numbers.push([Dygraph.floatFormat(0.0, p),
                      0.0.toPrecision(p)]);

        var elem = document.getElementById('content');
        elem.innerHTML = '';
        elem.appendChild(createTable(headers, numbers));
      };

      document.getElementById('p_input').value = '4';
      updateTable();
    });
    //--><!]]></script>
  </body>
</html>