From: Xavier Noria Date: 2003-08-06T02:09:08+09:00 Subject: Re: format number with comma separators? On Tuesday 05 August 2003 18:07, Warren Brown wrote: > >> Yet another regex solution: > >> > >> % ruby -e 'puts "456778904".gsub(/(.)(?=.{3}+$)/, %q(\1,))' > >> 456,778,904 > >> > >> -- fxn > >> > >> .gsub(/(\d)(?=\d{3}+$)/, '\1,') > > > > That spurious line at the end uses a regex that works for negative > > numbers as well. > > If you want to allow a decimal point: > > irb(main):001:0> puts "-123456789.01".gsub(/(\d)(?=\d{3}+(\.\d*)?$)/, > '\1,') -123,456,789.01 But: irb(main):003:0> puts "-123456789.0123".gsub(/(\d)(?=\d{3}+(\.\d*)?$)/, '\1,') -123,456,789.0,123 Is that the expected behaviour? Which is the convention in the decimal part? -- fxn