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

Matt Berry
Matt Berry
8,400 Points

Float vs. Integer

When converting a string to a number, when and why might it be advantageous to use the .to_i method over the .to_f method? Doesn't .to_f allow for greater flexibility and include integer values?

1 Answer

Gavin Ralston
Gavin Ralston
28,770 Points

A very short and not comprehensive list:

  • When you're going to be passing those values to a function that takes an int
  • Whenever you don't need to use floating point math
  • Whenever you can somehow avoid using floating point math (as much as possible, in any language)