Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Ruby Ruby Basics Numbers Converting Strings to Numbers

Ali .
Ali .
15,521 Points

I need help, please.

We've assigned the string "123" to a variable named string. Take string, use a method to convert it to an integer, and store the result in a variable named integer. Then call a second method on string to convert it to a floating-point number, and store the result in a variable named float.

program.rb
string = "123"
# YOUR CODE HERE

5 Answers

string = "123"
# Now convert 'string' to an integer and store it in a variable called 'integer'
integer = string.to_i
# => 123

# Remember that in programming, we read from right to left when assigning variables
# That was very helpful to me to think about when I was just beginning.

# Now, convert 'integer' into a floating point number and store it in a variable called float
# Based on the previous answer, I'll bet you can guess how to convert an integer to a float.
# Yup, '#to_f' should do the trick

float = integer.to_f
# => 123.00
# Now the variable float should contain a floating point number 123.00
Ali .
Ali .
15,521 Points

Thank you, Jeremy . I appreciate it.

Glad to help. Stick with it, it's worth the effort!

Dan Farrell
Dan Farrell
4,124 Points

just a heads up, the actual final answer for this is

string = "123"

# firstly convert 'string' to an integer using '.to_i' and store it in a variable called 'integer'
integer = string.to_i

# then, convert 'string' to a 'floating number' using '.to_f' and store it in a variable called 'float'
float = string.to_f

Hope this helps somebody :)

Place the number '2' 


string = "2"

integer = string.to_i


float = integer.to_f