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

« 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)

   1  
   2  class Numeric
   3    def commify(dec='.', sep=',')
   4      num = to_s.sub(/\./, dec)
   5      dec = Regexp.escape dec
   6      num.reverse.gsub(/(\d\d\d)(?=\d)(?!\d*#{dec})/, "\\1#{sep}").reverse
   7    end
   8  end


So, you get:

   1  
   2  1233232423424.23423.commify # => "1,233,232,423,424.23"
« Newer Snippets
Older Snippets »
Showing 1-1 of 1 total  RSS