From: Austin Ziegler Date: 2003-12-09T05:43:49+09:00 Subject: Re: format money On Tue, 9 Dec 2003 04:22:08 +0900, Mark J. Reed wrote: > On Mon, Dec 08, 2003 at 07:06:32PM +0000, saggmannen wrote: > >> Hello, is there a way to format "Money"-style floats in ruby. E.g: >> >> >> 2111000.5756 should be written as the string "2,111,000.58" >> > > Yes, there is. I'm sure that there's a module in the RAA for this, > although I can't find it at the moment. It's not too hard to do > manually, though: > > irb(main):001:0> balance = 2111000.5756 > => 2111000.5756 > irb(main):002:0> balance = (balance * 100).round/100.0 => 2111000.58 > irb(main):003:0> balance.to_s.reverse.gsub(/\d{3}/, '\&,').reverse => > "2,111,000.58" http://www.rubygarden.org/ruby?FixNumFormat This has been superseded by the Standard Library Extensions that Gavin Sinclair has put together (he has used my code and cleaned up the interface a little to be more "language" oriented instead of parameter heavy). -austin -- austin ziegler * austin@halostatue.ca * Toronto, ON, Canada software designer * pragmatic programmer * 2003.12.08 * 15.41.44