From: William James Date: 2005-02-13T06:19:59+09:00 Subject: Re: Currency formatting with regexp Andrew Johnson wrote: > On Sat, 12 Feb 2005 09:32:49 +0900, Mark Hubbart > wrote: > [snip] > > > golf? > > > > def commify(num) > > num.to_s.reverse.scan(/..?.?/).join(",").reverse > > end > > That one doesn't do well with floats, which was what the poster > was dealing with. I think this works properly: * def commify( n ) * n.to_s.reverse.gsub(/(\d{3})(?=\d+-?$)/,'\1,').reverse * end