Locked lesson.
About this lesson
Understanding the difference between floats and integers and when to use each.
Exercise files
Download this lesson’s related exercise files.
Floats and Integers.docx58.6 KB Floats and Integers - Solution.docx
58.7 KB
Quick reference
Floats and Integers
When it comes to numbers, choose between floats and integers.
When to use
Use floats or integers depending on what you're trying to accomplish.
Instructions
Floats are decimals, like 19.95
Integers are whole numbers, like 12 or 41
Hints & tips
- Floats use decimals
- Integers are whole numbers
- 00:04 In this video, I want to talk about floats and integers, integers and floats.
- 00:08 In the last video we talked about math.
- 00:10 Math obviously deals with numbers.
- 00:12 Ruby handles numbers a couple of different ways, as an integer or a float.
- 00:16 So what's the difference between the two?
- 00:18 Well, an integer is just a round number.
- 00:20 41, 12, 0, those are just integers, right?
- 00:27 A float, on the other hand, is 19.99.
- 00:30 It has decimals, so any number with decimals is a float.
- 00:34 12.55, that's a float.
- 00:36 So there's a very real difference between the two, and
- 00:39 it's actually pretty important.
- 00:40 And a lot of times you need to know whether you're dealing with an integer
- 00:43 or a float.
- 00:44 So why is this important?
- 00:45 Well, let's go puts 10 divided by 3.
- 00:50 Now, if I pull up my calculator, and I take 10 divided by 3,
- 00:56 you see the answer is 3.3333333, right, to infinity.
- 01:02 Well, if we run it here, we save this and run our program, we see the answer is 3.
- 01:07 And we know that's just not true.
- 01:08 But what Ruby is doing here is Ruby is going okay, they want to use integers
- 01:13 because they didn't use decimal points, so I'm going to return an integer.
- 01:17 And it returns 3.
- 01:18 So there are certain times when you may want to do that.
- 01:21 There are very legitimate reasons to want to do that.
- 01:24 Other times, you're going to want to be more exact.
- 01:26 And in that case, you're going to use floats.
- 01:28 So to change this to a float, you just add decimals.
- 01:32 Go ahead and save this and run it.
- 01:35 And we get 3.33333, and it rounds up to 5.
- 01:38 So those are floats, those are integers, very important.
- 01:42 So one kind of weird thing that you have
- 01:45 to be aware of when it comes to floats is this.
- 01:49 Let's go ahead and let's go puts, and let's go 0.05 plus 1.
- 01:53 So you would expect the answer to be 1.05.
- 01:56 But you see right away we're getting this big angry red X, and
- 01:59 we know that's an error.
- 02:00 And the error message says floating literal anymore put 0 before dot, okay?
- 02:06 So if we run this, well, we already know, oops, I've gotta save this first.
- 02:10 If we save it and run it, we already know we're going to get an error.
- 02:13 But of course, we see the same exact thing.
- 02:15 And I talked a couple of videos ago about errors, and
- 02:18 this one even tells you what to do.
- 02:20 It says put 0 before dot.
- 02:21 And that's the problem.
- 02:22 Floats require 0 to begin with, 0.05 something, and
- 02:27 that has to do with the float method.
- 02:30 Then, it looks for a single integer followed by decimal.
- 02:34 But just remember, whenever you're dealing with below one decimals,
- 02:38 you always have to put a 0 in front.
- 02:39 Now, if we save this and run it, we get 1.05, just like that.
- 02:44 So that's interesting.
- 02:46 And you can play around with this a little bit.
- 02:47 You could see, if we save this and run it, if only one of the numbers
- 02:52 is an integer and one or them is a float, we still get a thing.
- 02:57 If we switch it around 10, 3.0, we save this and run it, again, same thing.
- 03:04 Doesn't matter how many decimals you go out, going to get the same answer.
- 03:08 So those are integers, those are floats.
- 03:10 In the next video, we're going to look at comparison operators.
Lesson notes are only available for subscribers.