Created
December 20, 2013 08:47
-
-
Save manufaktor/8052102 to your computer and use it in GitHub Desktop.
format number
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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