Ruby Integer to_i function with example Last Updated : 12 Jul, 2025 Comments Improve Suggest changes Like Article Like Report 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: Ruby # 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 Output: 51 10 1690 183 Example #2: Ruby # 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 Output: 312 98 190 1312 Comment More infoAdvertise with us Next Article Ruby Integer times function with example G gopaldave Follow Improve Article Tags : Ruby Ruby-Methods Ruby Integer-class Similar Reads Ruby Integer to_int function with example 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 valu 1 min read Ruby Integer to_r function with example The to_r function in Ruby converts the value of the number to a rational. Syntax: number.to_r Parameter: The function takes the number which is to be converted to a rational number. Return Value: The function returns the rational number. Example #1: Ruby # Ruby program for to_r function # Initializi 1 min read Ruby Integer to_s function with example The to_s function in Ruby returns a string containing the place-value representation of int with radix base (between 2 and 36). If no base is provided in the parameter then it assumes the base to be 10 and returns. Syntax: number.to_s(base) Parameter: The function takes the number and a base to whi 1 min read Ruby Integer to_f function with example The to_f function in Ruby converts the value of the number as a float. If it does not fit in float, then it returns infinity. Syntax: number.to_f Parameter: The function takes the integer which is to be converted to float. Return Value: The function returns the float value of the number. Example #1: 1 min read Ruby Integer times function with example The times function in Ruby returns all the numbers from 0 to one less than the number itself. It iterates the given block, passing in increasing values from 0 up to the limit. If no block is given, an Enumerator is returned instead. Syntax: (number).times Parameter: The function takes the integer ti 2 min read Ruby Integer gcd() function with example The gcd() function in Ruby returns the gcd of two numbers. GCD signifies the greatest common divisor which divides both the numbers. Syntax: number1.gcd(number2) Parameter: The function requires two numbers whose gcd is to be returned. Return Value: The function returns the gcd of two numbers Exampl 1 min read Like