Skip to content

Instantly share code, notes, and snippets.

@jfsiii
Created September 21, 2009 00:14
Show Gist options
  • Save jfsiii/189996 to your computer and use it in GitHub Desktop.
Save jfsiii/189996 to your computer and use it in GitHub Desktop.
Dollar functions
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