From: saggmannen@... (saggmannen) Date: 2003-12-09T22:22:01+09:00 Subject: Re: format money Gregory Millam wrote in message news:<20031208154352.7194ada5.walker@lethalcode.net>... > Received: Tue, 9 Dec 2003 05:39:27 +0900 > And lo Peter wrote: > > > > # Here's my try: > > > > > > balance.to_s.reverse.gsub(/(\d{3})(?=\d)/,'\1,').reverse > > > > > > examples: > > > irb(main):007:0> "123456789".reverse.gsub(/(\d{3})(?=\d)/,'\1,').reverse > > > => "123,456,789" > > > irb(main):008:0> "1234567890".reverse.gsub(/(\d{3})(?=\d)/,'\1,').reverse > > > => "1,234,567,890" > > > > > > (?=\d) is a lookahead assertion that checks to see if there's a digit > > > ahead of the current pattern without capturing it (which is the > > > important thing). > > > > Apparently I hid my try pretty well in my reply :-) > > > > Peter > > *hits forehead* > > Because of the ">" in irb(main):...:..>, my mua displays it as a comment. > *continues to hit forehead* > > > - Greg 'blind' Millam both your solutions work fine if the number of decimals is less than or equal to three. What if I would like more precision than that in my output? Eg. 2111000.5567 => "2,111,000.5567"