Rounding float in Ruby

后端 未结 9 1205
北恋
北恋 2020-12-12 13:33

I\'m having problems rounding. I have a float, which I want to round to the hundredth of a decimal. However, I can only use .round which basically turns it in

9条回答
  •  萌比男神i
    2020-12-12 13:56

    def rounding(float,precision)
        return ((float * 10**precision).round.to_f) / (10**precision)
    end
    

提交回复
热议问题