Skip to content

Instantly share code, notes, and snippets.

@manufaktor
Created December 20, 2013 08:47
Show Gist options
  • Save manufaktor/8052102 to your computer and use it in GitHub Desktop.
Save manufaktor/8052102 to your computer and use it in GitHub Desktop.
format number
Number.prototype.toCurrency = function() {
var value;
if (isNaN(this) || !isFinite(this)) {
return '-';
}
value = Math.abs(this).toFixed(2);
value = value.replace(/(\d)(?=(\d\d\d)+(?!\d))/g, '$1,');
return (this < 0 ? '-$' : '$') + value;
};
Number.prototype.toPercent = function() {
if (isNaN(this) || !isFinite(this)) {
return '-';
}
return Math.abs(this * 100).toFixed(2) + '%';
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment