Ruby Integer to_i function with example
Last Updated :
07 Jan, 2020
The to_i function in Ruby converts the value of the number to int. If the number itself is an int, it returns self only.
Syntax: number.to_i
Parameter: The function takes the number which is to be converted to int.
Return Value: The function returns the int value.
Example #1:
num1 = 51
num2 = 10 . 78
num3 = 1690 . 89
num4 = 183
puts num1.to_i
puts num2.to_i
puts num3.to_i
puts num4.to_i
|
Output:
51
10
1690
183
Example #2:
num1 = 312
num2 = 98 . 09
num3 = 190 . 23
num4 = 1312
puts num1.to_i
puts num2.to_i
puts num3.to_i
puts num4.to_i
|
Output:
312
98
190
1312
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...