On Sat, 12 Feb 2005 09:32:49 +0900, Mark Hubbart <discordantus / gmail.com>
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.

As an aside, the commify routine from the Perl FAQ (shown earlier)
was actually designed as a means of commifying multiple numbers
in a string in a single pass:

  str = "123456789.987654321 and $1500.00 and 3.14159"
  p str.reverse.gsub(/(\d\d\d)(?=\d)(?!\d*\.)/,'\1,').reverse

So if, for example, you were pulling numbers from a data source
and creating an output string of a series of LaTeX tables, you
could build the entire string first with the raw numbers and
commify it all at once after the fact -- much more efficient than
commifying each number as you build the string. (this was, in
fact, the use-case that led to this particular solution).

regards,
andrew

-- 
Andrew L. Johnson   http://www.siaris.net/
      In theory, there's no difference between
      theory and practice, but in practice there is!