From: Gavin Sinclair Date: 2003-12-09T23:11:02+09:00 Subject: Re: format money On Wednesday, December 10, 2003, 12:52:01 AM, saggmannen wrote: > Gavin Sinclair wrote in message > news:<147830544870.20031209075430@soyabean.com.au>... >> On Tuesday, December 9, 2003, 6:07:02 AM, 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" >> >> > Maybe even more configurable with the Money-style class even taking >> > locale into account, or at least letting the user define what kind of >> > tokens to use as thousand-separator and decimal-token. This can be >> > done in C++ (even if it's kind of complicated). Has it been done in >> > ruby? >> >> http://extensions.rubyforge.org defines Numeric#format_s (originally >> written by Austin Ziegler) which gives you control over the characters >> used for separator, and everything else. >> >> Gavin > Gavin! I took a look at Numeric#format_s. I'm thinking this function > is doing too much. Wouldn't it be better to separate the setup of the > "locale" from the actual formatting of the "Money"? Because the setup > is something you probably do once, but you may want to output many, > many moonies. This probably means that this can't be a member of > "Numeric" any longer. Rather the format function has to be a member of > a separate "NumericFormat"-class (initialized with the proper > "locale"). what do you think? Yep, that's quite right. Such functionality really belongs in the context of a formatting package, not an extensions package. But for convenience... Our hypothetical format package would also include lots of methods for formatting strings (wrapping etc.) and would generally be quite useful. Gavin