| 1 | Gallery.register( |
| 2 | 'number-format', |
| 3 | { |
| 4 | name: 'Number formatting', |
| 5 | setup: function(parent) { |
| 6 | parent.innerHTML = |
| 7 | "<p>The default formatting mimicks printf with %.<i>p</i>g where <i>p</i> is" + |
| 8 | " the precision to use. It turns out that JavaScript's toPrecision()" + |
| 9 | " method is almost but not exactly equal to %g; they differ for values" + |
| 10 | " with small absolute values (10^-1 to 10^-5 or so), with toPrecision()" + |
| 11 | " yielding strings that are longer than they should be (i.e. using fixed" + |
| 12 | " point where %g would use exponential).</p>" + |
| 13 | |
| 14 | "<p>This test is intended to check that our formatting works properly for a" + |
| 15 | " variety of precisions.</p>" + |
| 16 | |
| 17 | "<p>Precision to use (1 to 21):" + |
| 18 | " <input type='text' id='p_input' size='20'></p>" + |
| 19 | "<p/>" + |
| 20 | "<div id='content' style='font-family:Courier New,monospace'></div>"; |
| 21 | }, |
| 22 | run: function() { |
| 23 | // Helper functions for generating an HTML table for holding the test |
| 24 | // results. |
| 25 | createRow = function(columnType, columns) { |
| 26 | var row = document.createElement('tr'); |
| 27 | for (var i = 0; i < columns.length; i ++) { |
| 28 | var th = document.createElement(columnType); |
| 29 | var text = document.createTextNode(columns[i]); |
| 30 | th.appendChild(text); |
| 31 | row.appendChild(th); |
| 32 | }; |
| 33 | return row; |
| 34 | }; |
| 35 | |
| 36 | createHeaderRow = function(columns) { |
| 37 | return createRow('th', columns); |
| 38 | }; |
| 39 | |
| 40 | createDataRow = function(columns) { |
| 41 | return createRow('td', columns); |
| 42 | }; |
| 43 | |
| 44 | createTable = function(headerColumns, dataColumnsList) { |
| 45 | var table = document.createElement('table'); |
| 46 | table.appendChild(createHeaderRow(headerColumns)); |
| 47 | for (var i = 0; i < dataColumnsList.length; i++) { |
| 48 | table.appendChild(createDataRow(dataColumnsList[i])); |
| 49 | } |
| 50 | return table; |
| 51 | }; |
| 52 | |
| 53 | updateTable = function() { |
| 54 | var headers = ['Dygraph.floatFormat()', 'toPrecision()', |
| 55 | 'Dygraph.floatFormat()', 'toPrecision()']; |
| 56 | var numbers = []; |
| 57 | var p = parseInt(document.getElementById('p_input').value); |
| 58 | |
| 59 | for (var i = -10; i <= 10; i++) { |
| 60 | var n = Math.pow(10, i); |
| 61 | numbers.push([Dygraph.floatFormat(n, p), |
| 62 | n.toPrecision(p), |
| 63 | Dygraph.floatFormat(Math.PI * n, p), |
| 64 | (Math.PI * n).toPrecision(p)]); |
| 65 | } |
| 66 | |
| 67 | // Check exact values of 0. |
| 68 | numbers.push([Dygraph.floatFormat(0.0, p), |
| 69 | 0.0.toPrecision(p)]); |
| 70 | |
| 71 | var elem = document.getElementById('content'); |
| 72 | elem.innerHTML = ''; |
| 73 | elem.appendChild(createTable(headers, numbers)); |
| 74 | }; |
| 75 | |
| 76 | document.getElementById('p_input').value = '4'; |
| 77 | document.getElementById('p_input').onchange = updateTable; |
| 78 | updateTable(); |
| 79 | } |
| 80 | }); |