Kevin Brown <blargity / gmail.com> 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