[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Searching for a method...

Meino Christian Cramer

10/4/2003 4:12:00 PM

4 Answers

simon

10/4/2003 4:18:00 PM

0

Meino Christian Cramer <Meino.Cramer@gmx.de> writes:
> Is there a method/function for "casting" floats to integers ?

% ri Float.to_i
------------------------------------------------------------- Float#to_i
flt.to_i -> anInteger
------------------------------------------------------------------------
Returns flt truncated to an Integer.


--
But it's all right; I'm catching the next train home.

ts

10/4/2003 4:25:00 PM

0

>>>>> "M" == Meino Christian Cramer <Meino.Cramer@gmx.de> writes:

M> Is there a method/function for "casting" floats to integers ?

Float#to_i, #ceil, #floor and #round


Guy Decoux



ahoward

10/4/2003 4:34:00 PM

0

Kevin Bullock

10/15/2003 6:18:00 PM

0

In <Pine.LNX.4.53.0310041634120.2132@eli.fsl.noaa.gov> Ara.T.Howard
wrote:
> On Sun, 5 Oct 2003, Meino Christian Cramer wrote:
>> Is there a method/function for "casting" floats to integers ?
>
> Integer(3.14)

There is also a method of Float:

irb(main):008:0> 3.14.to_i
3

>> Something like ( int)(Math.sqrt(2)+1) ???

irb(main):010:0> ((Math.sqrt 2) + 1).to_i
2

Alternatively:

irb(main):011:0> ((Math.sqrt 2) + 1).ceil
3
irb(main):012:0> ((Math.sqrt 2) + 1).floor
2

Depending on what you want out of the float into your integer.