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 (Retired) Ruby Numbers Multiplication and Division

Tucker Sowers
Tucker Sowers
4,539 Points

Why is 8/5 = 1? I am guessing ruby is rounding, but this was not discussed. Thank you.

Why is 8/5 = 1? I am guessing ruby is rounding, but this was not discussed. Thank you.

1 Answer

Jonathan Chua
Jonathan Chua
4,136 Points

You are using integers (whole numbers) in your equation, so Ruby automatically outputs an integer. An easy way to get a float (decimal number) is to change one of the numbers to a float. For example: 8.0.

Tucker Sowers
Tucker Sowers
4,539 Points

This makes a lot of sense. Funny that no one mentioned that. Thank you very much.

Jonathan is correct. Since everything in Ruby is an object, what's happening is that you're calling the / (divide) method on 8, which is a Fixnum (aka an integer). That method looks to see if you're passing in another class of number with higher precision (like a Float), and returns that higher precision number.

Likewise, if you called 8.0 / 5, you're calling the / (divide) method on 8.0 which is a Float. Since you're passing in 5 as the first parameter, it's going to do Float based math by default.