From: Austin Ziegler Date: 2006-12-21T06:21:54+09:00 Subject: Re: Formating Floats as Money On 11/25/06, Vidar Hokstad wrote: > Phrogz wrote: > > John Kopanas wrote: > > > I have a number that is of type float. I want to format it as dollars > > > and cents. How do you recommend I do it? :-) > > > > Here's my shot at it: > > > > class Numeric > > def to_currency( pre_symbol='$', thousands=',', decimal='.', > > post_symbol=nil ) > > "#{pre_symbol}#{ > > ( "%.2f" % self ).gsub( > > /(\d)(?=(?:\d{3})+(?:$|\.))/, > > "\\1#{thousands}" > > ) > > }#{post_symbol}" > > end > > end > > Nice try. Now all it needs is to be able to handle other variations of > the number of digits between each separator, which may not be uniform > (i.e. 100,00,000.00), and it should be able to handle most > international formats. There's one of those on Rubyforge. Gavin Sinclair has an "extensions" library to which I contributed my solution some time back. -austin -- Austin Ziegler * halostatue@gmail.com * http://www.halostatue.ca/ * austin@halostatue.ca * http://www.halostatue.ca/feed/ * austin@zieglers.ca