Skip to content

Instantly share code, notes, and snippets.

@stevedev
Created June 26, 2012 15:55
Show Gist options
  • Save stevedev/2996649 to your computer and use it in GitHub Desktop.
Save stevedev/2996649 to your computer and use it in GitHub Desktop.
addCommas
Object.prototype.addCommas = ->
[left, right] = this.toString().split('.')
right = '.' + right if right?
right ?= ''
regex = /(\d+)(\d{3})/
left = left.replace(regex, '$1' + ',' + '$2') while (regex.test(left))
left + right
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment