Skip to content

Instantly share code, notes, and snippets.

@lsmith
Created March 19, 2009 00:35
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lsmith/81507 to your computer and use it in GitHub Desktop.
Save lsmith/81507 to your computer and use it in GitHub Desktop.
function formatNumber(n) {
if (!isFinite(n)) {
return n;
}
var s = ""+n, abs = Math.abs(n), _, i;
if (abs >= 1000) {
_ = (""+abs).split(/\./);
i = _[0].length % 3 || 3;
_[0] = s.slice(0,i + (n < 0)) +
_[0].slice(i).replace(/(\d{3})/g,',$1');
s = _.join('.');
}
return s;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment