Ruby Integer to_i function with example

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:

filter_none

edit
close

play_arrow

link
brightness_4
code

# Ruby program for to_i function 
   
# Initializing the number
num1 = 51
num2 = 10.78
num3 = 1690.89
num4 = 183
    
# Prints the number as int
puts num1.to_i
puts num2.to_i
puts num3.to_i
puts num4.to_i

chevron_right


Output:

51
10
1690
183

Example #2:

filter_none

edit
close

play_arrow

link
brightness_4
code

# Ruby program for to_i function 
   
# Initializing the number
num1 = 312
num2 = 98.09
num3 = 190.23
num4 = 1312
   
# Prints the number as int
puts num1.to_i
puts num2.to_i
puts num3.to_i
puts num4.to_i

chevron_right


Output:

312
98
190
1312


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.