convert variable from string to float in Ruby
Code snippet on how to convert variable from string to float in Ruby
num_string = "1.75"
num_float = num_string.to_f
In this code snippet, we are converting the string variable num_string
which contains the value “1.75”, into a float variable num_float
. To do this, we use the Ruby method to_f
which converts the string into a float. The resulting float variable is assigned to the variable num_float
, which can then be used in any calculations or logic.