convert variable from float to string in Ruby
Code snippet on how to convert variable from float to string in Ruby
num = 3.14
str = num.to_s
This code converts the float variable ‘num’ which holds the value of 3.14 to a string ‘str’. First, we assign the float 3.14 to the variable ‘num’. Next, we use the ‘.to_s’ method on the ‘num’ variable to convert its value to a string; this value is then assigned to the variable ‘str’. Finally, ‘str’ will hold a string value of ‘3.14’. This code allows us to use a float variable ‘num’ and convert its value to a string value in the variable ‘str’.