DZone 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

Snippets has posted 5883 posts at DZone. View Full User Profile

Put Commas Into Long Integers

05.10.2005
| 856 views |
  • submit to reddit
        By Lucas Carlson - http://tech.rufy.com/

class Integer
    alias _to_s to_s
    def to_s
        _to_s.reverse.gsub(/(\d{3})/, '\1,').chomp(",").reverse
    end
end
    

Comments

Snippets Manager replied on Mon, 2012/05/07 - 2:12pm

Two comments: 1 A three-digit negative does not quite work as desired "-,315" instead of "-315". 2 I have to use "inspect" instead of "_to_s"" else I'm getting something like "#"