gpt4 book ai didi

ruby - 将范围转换为 Ruby 中的浮点值数组时出错

转载 作者:数据小太阳 更新时间:2023-10-29 07:21:10 26 4
gpt4 key购买 nike

如何将具有 startend 区间的 Range 转换为 Float 值?我收到错误,因为 TypeError: can't iterate from Float

IRB session

irb(main):058:0> (1..10).to_a
=> [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

irb(main):059:0> ('a'..'k').to_a
=> ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k"]

irb(main):061:0> ((1.1)..(1.10)).to_a
TypeError: can't iterate from Float
from (irb):61:in `each'
from (irb):61:in `to_a'
from (irb):61
.........

最佳答案

试试这个:

(1.1..1.2).step(0.01).map { |x| x.round(2) }
# => [1.1, 1.11, 1.12, 1.13, 1.14, 1.15, 1.16, 1.17, 1.18, 1.19, 1.2]

关于ruby - 将范围转换为 Ruby 中的浮点值数组时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21254219/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com