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.
Fc582698581884352e745d1d4c64699d?d=identicon&s=25 Joshua Muheim (josh)
on 2008-10-17 23: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
D6434aa0b7b350f8c3ed0119d81b2ead?d=identicon&s=25 Roy Pardee (rpardee)
on 2008-10-17 23: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
B14575f0ca69f10938fdd67e7156e0e1?d=identicon&s=25 Craig Demyanovich (Guest)
on 2008-10-17 23: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
Fc582698581884352e745d1d4c64699d?d=identicon&s=25 Joshua Muheim (josh)
on 2008-10-17 23:22
Thanks guys :-)
This topic is locked and can not be replied to.