Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

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 :)

Fradely Dilone
Fradely Dilone
24,037 Points
Place the number '2' 


string = "2"

integer = string.to_i


float = integer.to_f