首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >小数舍入到0.5的更好的Ruby实现

小数舍入到0.5的更好的Ruby实现
EN

Stack Overflow用户
提问于 2010-10-01 15:18:15
回答 3查看 8.2K关注 0票数 22

这似乎是可怕的低效。谁能给我一个更好的Ruby方式。

代码语言:javascript
复制
def round_value
  x = (self.value*10).round/10.0 # rounds to two decimal places
  r = x.modulo(x.floor) # finds remainder
  f = x.floor

  self.value = case
  when r.between?(0, 0.25)
    f
  when r.between?(0.26, 0.75)
    f+0.5
  when r.between?(0.76, 0.99)
    f+1.0
  end
end
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/3837182

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档