Created
September 21, 2009 00:14
-
-
Save jfsiii/189996 to your computer and use it in GitHub Desktop.
Dollar functions
This file contains 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.asDollar = function() | |
{ | |
return "$" + this.toFixed(2); | |
} | |
Number.prototype.asDollarWithCommas = function() | |
{ | |
var as_str = this.toFixed(2), | |
matches = as_str.match(/\.\d+$/), | |
cents = (matches && matches[0]), | |
dollars = [], | |
slice; | |
as_str = as_str.replace(cents, ''); | |
while (as_str.length > 3){ | |
dollars.push(slice = as_str.slice(-3)); | |
as_str = as_str.replace(slice, ''); | |
} | |
if (as_str.length){ dollars.push(as_str); } | |
return "$" + dollars.reverse().join(',') + cents; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment