From: Armando Padilla Date: 2007-12-02T02:04:59+09:00 Subject: Re: Float Question > I fail to see where the issue with using (s)printf is. If i use sprintf im assuming that the argument is changed to a string type. If then i chose to calculate additional values with this string type i would have to reconvert it to a float. On the surface its fine, i wouldnt mind doing it but I need speed and any less lines that ruby will translate too in terms of machine language is beneficial to me. I actually like Maddox truncate example. Thanks Maddox. i wasnt aware of both that feature and *.format Klemme suggested. Robert Klemme wrote: > On 01.12.2007 09:26, Armando Padilla wrote: >> Hey Chris thanks for the feedback. Yea im using that but i was >> looking for something built into the Float stack. > > Class != stack. > >> Something along the lines of Float.new(> here>). Basically something without the need to use sprintf. > > I fail to see where the issue with using (s)printf is. Anyway, if you > do not like the syntax of sprintf you can as well do this: > > irb(main):001:0> f=1.0/3 > => 0.333333333333333 > irb(main):002:0> "%.2f" % f > => "0.33" > >> Next release of Ruby request? lol > > Probably not. As others have demonstrated, you can easily do this in > Ruby today. But it seems, few people know the star operator in printf > format strings. You can do this: > > irb(main):021:0> module Precision > irb(main):022:1> def format(decimals,total=0) > irb(main):023:2> sprintf "%*.*f", total, decimals, self > irb(main):024:2> end > irb(main):025:1> end > => nil > irb(main):026:0> f=1.0/3 > => 0.333333333333333 > irb(main):027:0> f.format 3 > => "0.333" > irb(main):028:0> f.format 3,10 > => " 0.333" > > Kind regards > > robert