I have a string \"1/16\" I want to convert it to float and multiply it by 45. However, I dont get the desired results.
\"1/16\"
45
I am trying in
@Farrel was right, and since Ruby 1.9 includes Rational and String has a to_r-method things are easier:
to_r
puts ("1/16".to_r * 45).to_f #=> 2.8125 puts ("1/16".to_r * 45).to_f.round(2) #=> 2.81
In 2.0 it became even easier with a rational literal:
1/16r # => (1/16)