public
Last active

  • Download Gist
format_number.js
JavaScript
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
function format_number(n,prec,dec,sep) {
// Original by Luke Smith (http://lucassmith.name)
 
n = !isFinite(+n) ? 0 : +n;
prec = !isFinite(+prec) ? 2 : Math.abs(prec);
sep = sep == undefined ? ',' : sep;
 
var s = n.toFixed(prec),
abs = Math.abs(n).toFixed(prec),
_, i;
 
if (abs > 1000) {
_ = abs.split(/\D/);
i = _[0].length % 3 || 3;
 
_[0] = s.slice(0,i + (n < 0)) +
_[0].slice(i).replace(/(\d{3})/g, sep+'$1');
 
s = _.join(dec || '.');
}
 
return s;
}

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.