Rounding float in Ruby

后端 未结 9 1211
北恋
北恋 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:53

    When displaying, you can use (for example)

    >> '%.2f' % 2.3465
    => "2.35"
    

    If you want to store it rounded, you can use

    >> (2.3465*100).round / 100.0
    => 2.35
    

提交回复
热议问题