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 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:
Ruby
# 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 ) |
Output:
10 16 100101010 267
Example #2:
Ruby
# 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 |
Output:
440 99 10000100 8
Contact Us