Ruby Integer to_s function with example

The to_i 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 which is to be converted. If no base is provided in the parameter, then the default parameter is assumed to be 10.



Return Value: The function returns a string containing the place-value representation of int with the given radix base.

Example #1:

filter_none

edit
close

play_arrow

link
brightness_4
code

# Ruby program for to_s function 
   
# Initializing the number
num1 = 10
num2 = 16
num3 = 298
num4 = 183
    
# Prints the string after 
# conversion into base 
puts num1.to_s
puts num2.to_s
puts num3.to_s(2)
puts num4.to_s(8)

chevron_right


Output:

10
16
100101010
267

Example #2:

filter_none

edit
close

play_arrow

link
brightness_4
code

# Ruby program for to_s function 
   
# Initializing the number
num1 = 120
num2 = 189
num3 = 132
num4 = 8
    
# Prints the string after 
# conversion into base 
puts num1.to_s(5)
puts num2.to_s(20)
puts num3.to_s(2)
puts num4.to_s

chevron_right


Output:

440
99
10000100
8


My Personal Notes arrow_drop_up

The function of education is to teach one to think intensively and to think critically

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.