For example:
9 / 5 #=> 1
but I expected 1.8
. How can I get the correct decimal (non-integer) result? Why is it returning 1
at all?
divisionfloating-pointinteger-divisionmathruby
For example:
9 / 5 #=> 1
but I expected 1.8
. How can I get the correct decimal (non-integer) result? Why is it returning 1
at all?
Best Answer
It’s doing integer division. You can use
to_f
to force things into floating-point mode:This also works if your values are variables instead of literals. Converting one value to a float is sufficient to coerce the whole expression to floating point arithmetic.