faqts : Computers : Programming : Languages : JavaScript : Language Core : Numbers

+ Search
Add Entry AlertManage Folder Edit Entry Add page to http://del.icio.us/
Did You Find This Entry Useful?

34 of 54 people (63%) answered Yes
Recently 7 of 10 people (70%) answered Yes

Entry

How can I format a number to a currency value(1,234,567,890.00)?

Mar 24th, 2000 16:07
Martin Honnen,


Use the round function of
http://www.faqts.com/knowledge-base/view.phtml/aid/1157/fid/209/lang/
plus the following function:

function format1000 (n, d) {
  n = round (n, d);
  for (var i = n.indexOf('.') - 3; i > 0; i -= 3)
    n = n.substring(0, i) + ',' + n.substring(i);
  return n;
}

Examples:
  alert(format1000(1234567890))
  alert(format1000(1234.12345, 4))