Skip to content

Instantly share code, notes, and snippets.

@alissonsales
Created June 10, 2009 16:37
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alissonsales/127333 to your computer and use it in GitHub Desktop.
Save alissonsales/127333 to your computer and use it in GitHub Desktop.
/*
f = número de casas decimais
d = separador de decimos (’,’ virgula por padrão)
t = separador de milhar (’.’ ponto por padrão)
*/
String.prototype.currencyFormat = function (f, d, t) {
var n = (n = this.match(/\d/g)) ? n.join('').replace(/^0+/,'') : '0', f = (f) ? f : 2, d = (d) ? d : ',', t = (t) ? t : '.';
if (n.length < f + 1) return '0' + d + ((n.length < f) ? new Array(f - n.length + 1).join('0') + n : n)
else return n.substr(0, n.length - f).split('').reverse().join('').match(/\d{1,3}/g).join(t).split('').reverse().join('') + d + n.substr(n.length - f)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment