From: "Brian Schröder" Date: 2005-09-13T22:22:24+09:00 Subject: Re: Quick question. On 13/09/05, Robert Klemme wrote: > Kevin Brown wrote: > > Alright, I feel like an absolute idiot, but the question is simple, > > and 30 minutes of looking hasn't yet yeilded the answer. > > > > I want to take a float (say 0.0) and output it with a space in front > > if it's positive, - in front if it's negative, and be able to specify > > how many numbers after the decimal get printed (padding with zeros). > > This will be used to output currency, and I just know there's a > > simple way to do it with Ruby, but not finding it. :-) > > > > Sorry to bother you all. > > >> [123.456,-123.456].map{|f| sprintf "% 3.2f", f} > => [" 123.46", "-123.46"] > > You can also use printf with the same format for direct output. > > Kind regards > > robert > > > Or you can use the % operator on a string which I like better. I.e. "%3.2f" % 1.0 #=> " 1.00" regards, Brian -- http://ruby.brian-schroeder.de/ Stringed instrument chords: http://chordlist.brian-schroeder.de/