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 trialAli .
15,521 PointsI 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.
string = "123"
# YOUR CODE HERE
5 Answers
Jeremy Gunter
14,235 Pointsstring = "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
Jeremy Gunter
14,235 PointsGlad to help. Stick with it, it's worth the effort!
Dan Farrell
4,124 Pointsjust 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 :)
Patrick Altman
6,173 PointsThank you
Fradely Dilone
24,037 PointsPlace the number '2'
string = "2"
integer = string.to_i
float = integer.to_f
Ali .
15,521 PointsAli .
15,521 PointsThank you, Jeremy . I appreciate it.