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

前端 未结 7 2084
悲哀的现实
悲哀的现实 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 15:01

    It’s doing integer division. You can make one of the numbers a Float by adding .0:

    9.0 / 5  #=> 1.8
    9 / 5.0  #=> 1.8
    

提交回复
热议问题