Rounding float in Ruby

后端 未结 9 1207
北恋
北恋 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条回答
  •  情书的邮戳
    2020-12-12 13:57

    You can also provide a negative number as an argument to the round method to round to the nearest multiple of 10, 100 and so on.

    # Round to the nearest multiple of 10. 
    12.3453.round(-1)       # Output: 10
    
    # Round to the nearest multiple of 100. 
    124.3453.round(-2)      # Output: 100
    

提交回复
热议问题