Why is division in Ruby returning an integer instead of decimal value?

前端 未结 7 2189
悲哀的现实
悲哀的现实 2020-11-22 14:13

For example:

9 / 5  #=> 1

but I expected 1.8. How can I get the correct decimal (non-integer) result? Why is it returning <

7条回答
  •  一个人的身影
    2020-11-22 14:40

    You can check it with irb:

    $ irb
    >> 2 / 3
    => 0
    >> 2.to_f / 3
    => 0.666666666666667
    >> 2 / 3.to_f
    => 0.666666666666667
    

提交回复
热议问题