Posted By

Leech on 07/21/06


Tagged

format money


Versions (?)

Who likes this?

11 people have marked this snippet as a favorite

mate
jkochis
kgosser
pocketkaos
powerkan
shachi
vali29
ckayra
mihael
sonnh
seanpowell


Format Money v1.0


 / Published in: JavaScript
 

URL: http://jsfromhell.com/number/fmt-money

Converts a number into the monetary format "123.456,78" without using loops. Created: 2005.08.08

  1. /**************************************
  2. * Jonas Raoni Soares Silva
  3. * http://www.joninhas.ath.cx
  4. **************************************/
  5.  
  6. fmtMoney = function(n, c, d, t){ //v1.0
  7. var m = (c = Math.abs(c) + 1 ? c : 2, d = d || ",", t = t || ".",
  8. /(\d+)(?:(\.\d+)|)/.exec(n + "")), x = m[1].length > 3 ? m[1].length % 3 : 0;
  9. return (x ? m[1].substr(0, x) + t : "") + m[1].substr(x).replace(/(\d{3})(?=\d)/g,
  10. "$1" + t) + (c ? d + (+m[2] || 0).toFixed(c).substr(2) : "");
  11. };

Report this snippet  

You need to login to post a comment.