From: John Feminella Date: 2011-02-11T05:34:23+09:00 Subject: Re: RCR feedback: Numeric#grouped Try this: ==== begin snippet ==== class Numeric def separate(sep=",") self.to_s.reverse.scan(/(?:\d*\.)?\d{1,3}-?/).join(sep).reverse end end 100.separate => "100" 100000.separate => "100,000" 1000000000.separate => "1,000,000,000" 1000000000.separate('.') => "1.000.000.000" 10000.12345.separate => "10,000.12345" ==== end snippet ==== ~ jf -- John Feminella Principal Consultant, BitsBuilder LI: http://www.linkedin.com/in/johnxf SO: http://stackoverflow.com/users/75170/ On Thu, Feb 10, 2011 at 14:59, Roger Pack wrote: > Hello all. > I notice in ruby there's an easy to way to input "separated" number: > > 1_000_000 > 1_000_000.0 > > But there doesn't seem to be an easy way to "separate" digits when > outputting.  At least I'm not aware of any. > The end goal would be something like > > print "human readable with commas: #{1_000_000.0.grouped(',')}" > > human readable with commas: 1,000,000.0 > > Does this already exist and, if not, any feedback (method name, > suitability) for its addition? > Thank you! > -r > > -- > Posted via http://www.ruby-forum.com/. > >