From: Vidar Hokstad Date: 2006-11-26T07:50:06+09:00 Subject: Re: Formating Floats as Money 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. Vidar