formatting - How can I format numbers as dollars currency string in JavaScript? -


i format price in javascript.
i'd function takes float argument , returns string formatted this:

"$ 2,500.00" 

what's best way this?

you can use:

  var profits=2489.8237   profits.tofixed(3) //returns 2489.824 (round up)   profits.tofixed(2) //returns 2489.82   profits.tofixed(7) //returns 2489.8237000 (padding) 

then can add sign of '$'.

if require ',' thousand can use:

number.prototype.formatmoney = function(c, d, t){ var n = this,      c = isnan(c = math.abs(c)) ? 2 : c,      d = d == undefined ? "." : d,      t = t == undefined ? "," : t,      s = n < 0 ? "-" : "",      = string(parseint(n = math.abs(number(n) || 0).tofixed(c))),      j = (j = i.length) > 3 ? j % 3 : 0;    return s + (j ? i.substr(0, j) + t : "") + i.substr(j).replace(/(\d{3})(?=\d)/g, "$1" + t) + (c ? d + math.abs(n - i).tofixed(c).slice(2) : "");  }; 

and use with:

(123456789.12345).formatmoney(2, '.', ','); 

if you're going use '.' , ',', can leave them off method call, , method default them you.

(123456789.12345).formatmoney(2); 

if culture has 2 symbols flipped (i.e. europeans), paste on following 2 lines in formatmoney method:

    d = d == undefined ? "," : d,      t = t == undefined ? "." : t,  

Comments

Popular posts from this blog

android - InAppBilling registering BroadcastReceiver in AndroidManifest -

python Tkinter Capturing keyboard events save as one single string -

sql server - Why does Linq-to-SQL add unnecessary COUNT()? -