带小数位的范围(Range with decimal place)

编程入门 行业动态 更新时间:2024-10-11 21:30:20
小数位的范围(Range with decimal place)

如何检查ruby中带小数位的范围?

我想在红宝石中过滤9.74 - 9.78之间的数组中的结果

if (9.74...9.78) == amounts count += 1 end

这似乎不起作用

How can I check a range with decimal place in ruby?

I want to filter results in an array between 9.74 - 9.78 in ruby

if (9.74...9.78) == amounts count += 1 end

This seems not to work

最满意答案

使用Range#cover执行此操作:

if (9.74...9.78).cover? amounts count += 1 end

示例:

p (9.74...9.78).cover? 9.75 # => true p (9.74...9.78).cover? 9.79 # => false

更新为@m_x建议

# will give you the element in the range array.select{ |item| (9.74..9.78).cover? item } # will give you the element count in the array belongs to the range array.count{ |item| (9.74..9.78).cover? item }

Do this using Range#cover:

if (9.74...9.78).cover? amounts count += 1 end

Example :

p (9.74...9.78).cover? 9.75 # => true p (9.74...9.78).cover? 9.79 # => false

Update as @m_x suggested

# will give you the element in the range array.select{ |item| (9.74..9.78).cover? item } # will give you the element count in the array belongs to the range array.count{ |item| (9.74..9.78).cover? item }

更多推荐

本文发布于:2023-07-27 08:21:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1287662.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:小数位   Range   place   decimal

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!