Browse Source

Nice formatting of amounts

Closes #7
master
Chris Smith 13 years ago
parent
commit
56037e5b1b
2 changed files with 15 additions and 4 deletions
  1. 14
    4
      analyser.js
  2. 1
    0
      index.html

+ 14
- 4
analyser.js View File

178
 
178
 
179
 // -----------------------------------------------------------------------------
179
 // -----------------------------------------------------------------------------
180
 
180
 
181
+/**
182
+ * Formats the specified number in a manner suitable for a currency. That is,
183
+ * fixed to two decimal places and with a thousand separator every 3 digits.
184
+ *
185
+ * @return A string representation of the number as a currency
186
+ */
187
+Number.prototype.toCurrency = function() {
188
+ return this.toFixed(2).replace(/([0-9])(?=([0-9]{3})+\.)/g, '$1,');
189
+};
190
+
181
 /**
191
 /**
182
  * Adds an 'alt' class to every other visible row in the specified table.
192
  * Adds an 'alt' class to every other visible row in the specified table.
183
  *
193
  *
245
    $('.hidden' + id).show();
255
    $('.hidden' + id).show();
246
    var handle = $('#collapseHandle' + id);
256
    var handle = $('#collapseHandle' + id);
247
    handle.text(handle.text().replace(/\+/, '-'));
257
    handle.text(handle.text().replace(/\+/, '-'));
248
-   handle.parents('tr').find('td.amount').text(handle.data('single'));
258
+   handle.parents('tr').find('td.amount').text(parseFloat(handle.data('single')).toCurrency());
249
   }
259
   }
250
  });
260
  });
251
 
261
 
255
    $('.hidden' + id).hide();
265
    $('.hidden' + id).hide();
256
    var handle = $('#collapseHandle' + id);
266
    var handle = $('#collapseHandle' + id);
257
    handle.text(handle.text().replace(/\-/, '+'));
267
    handle.text(handle.text().replace(/\-/, '+'));
258
-   handle.parents('tr').find('td.amount').text(handle.data('total'));
268
+   handle.parents('tr').find('td.amount').text(parseFloat(handle.data('total')).toCurrency());
259
   }
269
   }
260
  });
270
  });
261
 
271
 
362
   $('<td/>').text(this.Type ? this.Type : 'Other').appendTo(tr);
372
   $('<td/>').text(this.Type ? this.Type : 'Other').appendTo(tr);
363
   $('<td/>').text(this.Category ? this.Category : '').appendTo(tr);
373
   $('<td/>').text(this.Category ? this.Category : '').appendTo(tr);
364
   $('<td/>').addClass('desc').text(this.Description).appendTo(tr);
374
   $('<td/>').addClass('desc').text(this.Description).appendTo(tr);
365
-  $('<td/>').addClass('amount').text(this.Amount).appendTo(tr);
375
+  $('<td/>').addClass('amount').text(this.Amount.toCurrency()).appendTo(tr);
366
  });
376
  });
367
 
377
 
368
  var tr = $('<tr/>').addClass('data total').appendTo(table);
378
  var tr = $('<tr/>').addClass('data total').appendTo(table);
369
  $('<th colspan="4" class="total">Total</th>').appendTo(tr);
379
  $('<th colspan="4" class="total">Total</th>').appendTo(tr);
370
- $('<td class="amount"></td>').text(total).appendTo(tr);
380
+ $('<td class="amount"></td>').text(total.toCurrency()).appendTo(tr);
371
 
381
 
372
  colourTableRows(table);
382
  colourTableRows(table);
373
  drawCategoryPieChart(included, incoming);
383
  drawCategoryPieChart(included, incoming);

+ 1
- 0
index.html View File

27
    .link { color: blue; text-decoration: underline; cursor: pointer; }
27
    .link { color: blue; text-decoration: underline; cursor: pointer; }
28
    #historytable tr.collapsed { opacity: 0.6; }
28
    #historytable tr.collapsed { opacity: 0.6; }
29
    table tr.total td, table tr.total th { background-color: #666; color: #fff; }
29
    table tr.total td, table tr.total th { background-color: #666; color: #fff; }
30
+   .amount { text-align: right; }
30
   </style>
31
   </style>
31
  </head>
32
  </head>
32
  <body>
33
  <body>

Loading…
Cancel
Save