The to_int function in Ruby converts the value of the number to int. If the number itself is an int, it returns self only. It is an alias of to_i function.
Syntax: number.to_int
Parameter: The function takes the number which is to be converted to int.
Return Value: The function returns the int value.
Example #1:
# Ruby program for to_int function # Initializing the number num1 = 51num2 = 10.78num3 = 1690.89num4 = 183 # Prints the number as int puts num1.to_int puts num2.to_int puts num3.to_int puts num4.to_int |
chevron_right
filter_none
Output:
51 10 1690 183
Example #2:
# Ruby program for to_i function # Initializing the number num1 = 312num2 = 98.09num3 = 190.23num4 = 1312 # Prints the number as int puts num1.to_int puts num2.to_int puts num3.to_int puts num4.to_int |
chevron_right
filter_none
Output:
312 98 190 1312
Recommended Posts:
- Ruby Integer integer? function with example
- Ruby Integer next function with example
- Ruby | Integer << function
- Ruby | Integer <=> function
- Ruby Integer chr function with example
- Ruby Integer gcd() function with example
- Ruby | Integer >= function
- Ruby | Integer - function
- Ruby Integer odd? function with example
- Ruby Integer div() function with example
- Ruby Integer even? function with example
- Ruby Integer lcm() function with example
- Ruby Integer abs() function with example
- Ruby Integer magnitude function with example
- Ruby Integer modulo() function with example
- Ruby Integer to_s function with example
- Ruby Integer truncate() function with example
- Ruby Integer remainder() function with example
- Ruby Integer to_r function with example
- Ruby Integer to_i function with example
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.

