From: Barrie Jarman Date: 2006-03-01T01:48:38+09:00 Subject: Re: Formatting to "Thousands" Implemented this into the float class. Adds check to make sure 0 is handled properly and adds for two decimal places (sorry, probably wasn't specific enough before). def to_finance if self != 0 number = sprintf("%.2f", self) number.to_s.reverse.gsub!(/(\d\d\d)(?=\d)(?!\d*\.)/, '\1,').reverse else sprintf("%.2f", self) end end "James Edward Gray II" wrote in message news:47AC1DDA-5D15-461F-AEB2-40B5E23903B1@grayproductions.net... > On Feb 28, 2006, at 5:08 AM, Barrie Jarman wrote: > >> A colleague of mine wishes to format a float to include commas at >> thousand >> values, for example >> 12345.21 becomes >> 12,345.21 >> >> Is there any inbuilt functionality to format this, or are we better of >> writing something ourselves... > > It's not built-in, but it's also pretty easy to roll one: > > def commify( number ) > number.to_s.reverse.gsub!(/(\d\d\d)(?=\d)(?!\d*\.)/, '\1,').reverse > end > > Hope that helps. > > James Edward Gray II > > > -- Posted via NewsDemon.com - Premium Uncensored Newsgroup Service ------->>>>>>http://www.NewsDemon.com<<<<<<------ Unlimited Access, Anonymous Accounts, Uncensored Broadband Access