如何将Range having start和endinterval 转换为Float值?我收到错误了TypeError: can't iterate from Float
IRB会议
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
.........
Run Code Online (Sandbox Code Playgroud)
试试这个:
(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]
Run Code Online (Sandbox Code Playgroud)