class Numeric def commify(dec='.', sep=',') num = to_s.sub(/\./, dec) dec = Regexp.escape dec num.reverse.gsub(/(\d\d\d)(?=\d)(?!\d*#{dec})/, "\\1#{sep}").reverse end end
So, you get:
1233232423424.23423.commify # => "1,233,232,423,424.23"
12372 users tagging and storing useful source code snippets
Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world
class Numeric def commify(dec='.', sep=',') num = to_s.sub(/\./, dec) dec = Regexp.escape dec num.reverse.gsub(/(\d\d\d)(?=\d)(?!\d*#{dec})/, "\\1#{sep}").reverse end end
1233232423424.23423.commify # => "1,233,232,423,424.23"
You need to create an account or log in to post comments to this site.