Forum: Ruby on Rails 3 / 2 is 1?

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Joshua M. (Guest)
on 2008-10-18 01:13
Hi all

Why is 3/2=1 in a Rais view? How can I get Rails to output 1.5 as the
result?

Thanks
Josh
Roy P. (Guest)
on 2008-10-18 01:17
(Received via mailing list)
That's a common ruby gotcha.  Division of integers results in an
integer.  To fix, convert one or both numbers to floats with .to_f or
include a decimal place on the literal (e.g., 3.0/2)

HTH,

-Roy
Craig D. (Guest)
on 2008-10-18 01:18
(Received via mailing list)
At least one of the numbers needs to be a float. Here's an example of
script/console output:

Loading development environment (Rails 2.1.1)
>> 3/2
=> 1
>> 3.0/2
=> 1.5
>> 3/2.0
=> 1.5
>> 3.0/2.0
=> 1.5

In Ruby, division of integers returns only the integer part of the
result.

Craig
Joshua M. (Guest)
on 2008-10-18 01:22
Thanks guys :-)
This topic is locked and can not be replied to.