Working with decimals in Ruby on Rails 3

前端 未结 5 2052
夕颜
夕颜 2020-12-19 00:34

I\'m trying to calculate the average net price of a product. I have, in my Product model :total_sold and :total_net_revenue. Doing straight division in the method seems to a

5条回答
  •  醉话见心
    2020-12-19 00:59

    You need to convert your values that are in whole cents (integers) to floats (numbers with decimals) so that the math will result in a floating number instead of an integer.

    So in general it works like this:

    some_integer.to_f / some_other_integer.to_f  # returns a float
    

提交回复
热议问题