开发者

What's the best way to round a Ruby BigDecimal number to the nearest 0.05?

开发者 https://www.devze.com 2023-02-06 02:34 出处:网络
Ruby\'s BigDecimal #round method rounds numbers off to the nearest integer (and returns it as a fixnum frustratingly). Is there a built in method to allo开发者_开发百科w rounding to the nearest 0.05?

Ruby's BigDecimal #round method rounds numbers off to the nearest integer (and returns it as a fixnum frustratingly). Is there a built in method to allo开发者_开发百科w rounding to the nearest 0.05? I can't find one.

The best I have managed is

def round05(number)
  (BigDecimal.new (number * 20).round.to_s) /20
end
0

精彩评论

暂无评论...
验证码 换一张
取 消