Home »
Ruby »
Ruby Programs
Ruby program to convert the string into an integer
Ruby Example: Write a program to convert the string into an integer.
Submitted by Nidhi, on December 10, 2021
Problem Solution:
In this program, we will create a variable with a string value. Then we will convert a string value into an integer using the to_i() function.
Program/Source Code:
The source code to convert the string into an integer is given below. The given program is compiled and executed successfully.
# Ruby program to convert the
# string into an integer
str = "10.5";
result = str.to_i();
print "Result: ",result;
Output:
Result: 10
Explanation:
In the above program, we created a variable str with an initial value "10.5". Then we converted the value of str into integer using the to_i() function and printed the result.
Ruby Basic Programs »