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

Commify numbers (See related posts)

This is just the Ruby version of a known regular expression
to get comma-formatted numbers.


numbers = "10000000 1.345 -91245555.45 +6788876.224334 -2321232" 

numbers.reverse!
numbers.gsub!(/(\d\d\d)(?=\d)(?!\d*\.)/, '\1,')
puts numbers.reverse!



You need to create an account or log in to post comments to this site.


Click here to browse all 4860 code snippets

Related Posts