带小数位的范围(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 endThis 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 endExample :
p (9.74...9.78).cover? 9.75 # => true p (9.74...9.78).cover? 9.79 # => falseUpdate 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 }更多推荐
发布评论