From: ahoward Date: 2003-03-21T03:58:58+09:00 Subject: Re: Newbie question: 9/5=1 ? On Thu, 20 Mar 2003, Thomas Jollans wrote: > while learning ruby i wanted to program a simple fahrenheit to celsius > converter. for that i need 9/5 to be 1,8 (and not 1). how do i do that in > ruby? in ruby objects (every variable) knows it's type. thus, to say a = 9/5 in ruby, is to like saying int a = (int)9/(int)5; in C. eg. the result on an int divided by an int is an int. so in ruby, as in C, you will need to make sure you variables are typed correctly, eg. as floats. something like this def to_fahrenheit degrees_celsius ((9.0/5.0) * degrees_celsius.to_f) + 32.0 end def to_celsius degrees_fahrenheit (5.0/9.0) * (degrees_fahrenheit.to_f - 32.0) end p to_fahrenheit 0 # >> 32.0 p to_celsius 32 # >> 0.0 note that these methods return floats because at least one of the operands is a float. -a -- ==================================== | Ara Howard | NOAA Forecast Systems Laboratory | Information and Technology Services | Data Systems Group | R/FST 325 Broadway | Boulder, CO 80305-3328 | Email: ahoward@fsl.noaa.gov | Phone: 303-497-7238 | Fax: 303-497-7259 ====================================