3 / 2 is 1?

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

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

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

Thanks guys :slight_smile: