Never been to DZone Snippets before?

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

About this user

Dr Nic Williams http://drnicwilliams.com

« Newer Snippets
Older Snippets »
Showing 1-1 of 1 total  RSS 

Turn numbers into strings with thousands' separators

Turn numbers into strings with thousands' separators (from zenspider)

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"
« Newer Snippets
Older Snippets »
Showing 1-1 of 1 total  RSS