From: Pat Maddox Date: 2007-12-01T18:03:55+09:00 Subject: Re: Float Question Or even the current release! class Float def truncate(places = 0) return to_i if places == 0 sprintf( "%0.#{places}f", self).to_f end end irb(main):007:0> 234.2342342.truncate 2 => 234.23 irb(main):008:0> 234.2342342.truncate => 234 On Dec 1, 2007 12:30 AM, Armando Padilla wrote: > Hey Chris thanks for the feedback. Yea im using that but i was looking > for something built into the Float stack. Something along the lines of > Float.new(). Basically something > without the need to use sprintf. > > Next release of Ruby request? lol > > > > > Chris Cummer wrote: > > What you're looking for is sprintf() though I suspect you know that > > already from your comment below. Ie: > > > > sprintf( "%0.02f", 234.2342342) > > > > You deride this as "string manipulation" though in effect as soon as > > you display the number that's what you're doing, no matter where or > > why you want to display it. The number only exists as a true float as > > long as you don't look at it; as soon as you do, you've forced it to > > achieve stringness. > > > > Makes sense? > > > > > > On 30-Nov-07, at 11:29 PM, Armando Padilla wrote: > > > >> This is probably the stupidest question ive ever had to ask on any > >> board so im sorry lol. :-p > >> > >> Yes i've googled and found a solution but i cant believe that the > >> solution presented is correct or that the great minds behind ruby > >> did not include something like this. > >> > >> I want to get a float 2.3333 to display as 2.33 or 2.333 etc. > >> > >> Now, i read the manual, Numerical, Float, Math sections but could > >> not find a way to do this. Yes you can use string manipulation but > >> that just defeats the purpose of having something like this built > >> into the Float stack. > >> > >> ex. someFloat = Float.new(2) ==> 2.12 > >> someFloat = Float.new(5) => 2.12345 > >> > >> Any thoughts? > >> > >> > > > > > >