Round a ruby float up or down to the nearest 0.05

后端 未结 9 1765
日久生厌
日久生厌 2020-12-29 04:43

I\'m getting numbers like

2.36363636363636
4.567563
1.234566465448465
10.5857447736

How would I get Ruby to round these numbers up (or dow

9条回答
  •  时光取名叫无心
    2020-12-29 04:56

    Check this link out, I think it's what you need. Ruby rounding

    class Float
      def round_to(x)
        (self * 10**x).round.to_f / 10**x
      end
    
      def ceil_to(x)
        (self * 10**x).ceil.to_f / 10**x
      end
    
      def floor_to(x)
        (self * 10**x).floor.to_f / 10**x
      end
    end
    

提交回复
热议问题