From: Dominik Bathon Date: 2005-11-03T22:56:12+09:00 Subject: Re: Newbie: How to format a number to always show two decimals? On Thu, 03 Nov 2005 01:57:09 +0100, Alan Chen wrote: > mental@rydia.net wrote: >> Now, not being a former C programmer, you may not be so strongly >> tempted to write: >> >> sprintf("%s%s%s", a.zolum, b.fep, c.gorza) >> >> when you could have simply written: >> >> "#{a.zolum}#{b.fep}#{c.gorza}" >> >> ..but if you ever are, please be aware that it's worth TWO kittens. > > That reminds me, what are the options for nicely text formatted table > output? I usually use something like: > [a.zolum, b.feb, c.gorza].inject("") { |row,col| row.concat "%7s" % col > } > > Are there better alternatives? If your cols are strings: [a.zolum, b.feb, c.gorza].map { |col| col.rjust(7) }.join If they aren't you have to add to_s: [a.zolum, b.feb, c.gorza].map { |col| col.to_s.rjust(7) }.join Dominik