Skip to content

Instantly share code, notes, and snippets.

@enriquez
Created April 24, 2009 17:00
Show Gist options
  • Save enriquez/101210 to your computer and use it in GitHub Desktop.
Save enriquez/101210 to your computer and use it in GitHub Desktop.
module NumberFormat
def to_comma_separated
self.to_s.gsub(/(\d)(?=\d{3}+(\.\d*)?$)/, '\1,')
end
end
class Integer
include NumberFormat
end
class Float
include NumberFormat
end
123123123.to_comma_separated # => "123,123,123"
123123123.12.to_comma_separated # => "123,123,123.12"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment