To convert a float to a string in Ruby, you can use the to_s
method.
In this example,
3.14
to a variable float1
.to_s
method to convert the float to a string and store the result in a string variable str1
.str1
.float1 = 3.14
str1 = float1.to_s
puts 'String value is: ' + str1
String value is: 3.14
In this example,
10.5
to a variable float2
.to_s
method to convert the float to a string and store the result in a string variable str2
.str2
.float2 = 10.5
str2 = float2.to_s
puts 'String value is: ' + str2
String value is: 10.5
In this tutorial, we learned How to convert Float to String in Ruby language with well detailed examples.