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 trialStrahinja Rodic
1,212 PointsA few questions :)
Can i cast in Ruby? Integer into float and similar?
regards!
P.S. Scroll down a little bit to see my other questions, please. :)
5 Answers
Abigail Glunn
10,068 PointsYes, you can also write it out like you describe! For example:
Integer (10.898)
=> 10
And even use that to convert from binary:
Float (0b01110101)
=> 117
If you don't want a new line to be created you should use "print" instead of "puts" (they're otherwise the same). Similarly you need to add ".chomp" after "gets" in order to chomp off whitespace from the end of a user entry. See below!
print "What's the year? "
year = gets.chomp
puts "The year is #{year.to_i}."
print "Type how far into in the future you would like to go: "
future = gets.chomp
puts "In #{future} years, the year will be #{year.to_i + future.to_i}."
This blog post explains types/memory limits pretty well: http://blog.iriomk.com/post/98382401723/ruby-numeric-types-dos-and-do-nots
If you have more questions it'd be great if you could open up new discussions for them (and vote one of these best answer for the question already answered if it was helpful)! : D
Abigail Glunn
10,068 PointsHey! You can convert between classes by adding:
- .to_f (float)
- .to_i (integer)
- .to_s (string)
For instance 37.to_s returns the string "37"
That what you're looking for?
Strahinja Rodic
1,212 PointsThank you very much! :) I was considering that option(to create a new discussion), however, I was thinking like "I shouldn't spam that much :D" so I changed my mind :)
Thank you anyway! :)
Strahinja Rodic
1,212 PointsYes, that's what I was looking for. Thanks! :) Also, I was looking for, for instance like in programming language C(if you "speak" it, and I'll guess you do), there's casting like this: int a=10, b=3; And now, a/b=3 (since integer dividing), however, if I write my code something like this: (float) a/b, I will get decimal result.
I hope you will understand me. Thank you for your patience and fast answer! :)
EDIT: i tried that what you wrote and it works great! However, i was wondering if there is any other syntax that might replace those? And is there a memory limit for float, integer numbers? Something like 2^32-1(INT_MAX in C for 32bit OS) or something like that? For instance, in C, we have also double(double more memory for float, since float can remember precisely only 4-5 numbers after dot in decimals, double can 6-7 and long double can memorize about 15-16 numbers in decimal. :)
Thank you once again! :)
EDIT 2: Please check out my code and my console. Can somebody tell me why is "years" in new line? Since my code is: puts "In #{future} years, the year will be #{year.to_i + future.to_i}."
and there's no "\n" in my string, why does the part "puts "years, the year will be #{year.to_i + future.to_i}." in output goes in the new line? http://prntscr.com/989j8m
Thank you for your hospitality. :)
Abigail Glunn
10,068 PointsGlad that helped! :D