Hey all, In my code, it seems that when I divide two integers, the result is an integer. Is there any way to make it such that when I divide two integers, the result is a double? Any help appreciated, Jin _______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails
int1 / int2.to_f this will force it into a float. On Thu, 15 Dec 2005, Jin Lee wrote:> Hey all, > > In my code, it seems that when I divide two integers, the result is an > integer. Is there any way to make it such that when I divide two integers, > the result is a double? > > Any help appreciated, > > Jin >
Jin Lee wrote:> Hey all, > > In my code, it seems that when I divide two integers, the result is an > integer. Is there any way to make it such that when I divide two > integers, > the result is a double?No, you have to convert one of them to a float first: x.to_f/y -- Posted via http://www.ruby-forum.com/.
On 16/12/2005, at 11:27 AM, Jin Lee wrote:> In my code, it seems that when I divide two integers, the result is > an integer. Is there any way to make it such that when I divide two > integers, the result is a double?Ruby never automatically casts (except between Fixnum and Bignum), so you have to cast to float using Fixnum#to_f 1.to_f / 5.to_f # => 0.2 -- Phillip Hutchings phillip.hutchings-QrR4M9swfipWk0Htik3J/w@public.gmane.org
This might help:>> a = 1=> 1>> b = 2=> 2>> a / b=> 0>> a.to_f / b=> 0.5>> a / b.to_f=> 0.5>> a.to_f / b.to_f=> 0.5 Regards, Douglas
On 12/15/05, Jin Lee <jinslee-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > Hey all, > > In my code, it seems that when I divide two integers, the result is an > integer. Is there any way to make it such that when I divide two integers, > the result is a double? >a = 10 b = 3 a/b.to_f -Matt _______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails
On 12/16/05, Jin Lee <jinslee-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Hey all, > > In my code, it seems that when I divide two integers, the result is an > integer. Is there any way to make it such that when I divide two integers, > the result is a double? > > Any help appreciated,irb(main):001:0> 5/3 => 1 irb(main):002:0> 5.to_f/3 => 1.66666666666667 irb(main):003:0> 5*1.0/3 => 1.66666666666667 martin
victor
2005-Dec-16 16:18 UTC
Re: slightly OT - Ruby division - and there''s yet another way!
Jin Lee <jinslee@...> writes:> Hey all, > In my code, it seems that when I divide two integers, the result is an > integer. Is there any way to make it such that when I divide two > integers, the result is a double? > Any help appreciated, > JinApparently Float is not only a class, but also a method! I gather, then, that Ruby is sort of Lisp-2 (non-methods and methods have different namespaces). #> Float(3) 3.0 #> Float(22)/7 3.14285714285714
Rick Olson
2005-Dec-16 16:58 UTC
Re: Re: slightly OT - Ruby division - and there''s yet another way!
> Apparently Float is not only a class, but also a method! I gather, then, that > Ruby is sort of Lisp-2 (non-methods and methods have different namespaces). > > #> Float(3) > 3.0 > #> Float(22)/7 > 3.14285714285714#to_f is usually used instead: #> 3.to_f 3.0 #> 22.to_f / 7> 3.14285714285714-- rick http://techno-weenie.net