Ruby program to convert a numeric string into integer using to_i() function

Ruby Example: Write a program to convert a numeric string into integer using to_i() function.
Submitted by Nidhi, on January 06, 2022

Problem Solution:

In this program, we will create a string initialized with a numeric string. Then we will convert the numeric string into integer using to_i() and return the result.

Program/Source Code:

The source code to covert a numeric string into integer using the to_i() function is given below. The given program is compiled and executed successfully.

# Ruby program to covert a numeric string 
# into integer using to_i() function

str = "4536";

num = str.to_i();

print "Number is: ",num,"\n";

Output:

Number is: 4536

Explanation:

In the above program, we created a string str initialized with "4536". Then we used the to_i() function to convert the numeric string into integer and assigned the result to the num variable. After that, we printed the result.

Ruby Strings Programs »




Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.