我有:
steven$ irb
ruby-1.9.2-p180 :001 > foo = "256MB"
=> "256MB"
ruby-1.9.2-p180 :002 > interim_result = foo.slice(/\d+/).to_i
=> 256
ruby-1.9.2-p180 :003 > interim_result.class
=> Fixnum
ruby-1.9.2-p180 :004 > result = interim_result/1028
=> 0
我想result
成为0.25
。我怎样才能做到这一点?
是否有必要/可能强制interim_result.class
成为integer
?
请注意,以下不给出 0.25 的期望结果:
ruby-1.9.2-p180 :002 > interim_result = foo.slice(/\d+/).to_f
=> 256.0
ruby-1.9.2-p180 :003 > result = interim_result/1028
=> 0.2490272373540856
ruby-1.9.2-p180 :004 > result.round_to(2)
NoMethodError: undefined method `round_to' for 0.2490272373540856:Float
谢谢。