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
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
=> 13.0/2
=> 1.53/2.0
=> 1.53.0/2.0
=> 1.5
In Ruby, division of integers returns only the integer part of the
result.
Craig
Thanks guys
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.
Sponsor our Newsletter | Privacy Policy | Terms of Service | Remote Ruby Jobs