Locked lesson.
About this lesson
In this lesson, we discuss the differences between integers, floats, decimals, and doubles, and how decimal places may be affected based on the data type you choose.
Exercise files
Download this lesson’s related exercise files.
16 - Math Floats Vs. Ints.docx61.1 KB 16 - Math Floats Vs. Ints SOLUTION.docx
57.9 KB
Quick reference
Math Floats Vs. Ints
Integers have no decimal places. If you need decimals, you should use either Float, Decimal, or Double.
When to use
Use these whenever you need to calculate decimal places with your numbers.
Instructions
10 divided by 3 returns:
- Integer: 3
- Float: 3.3333333
- Decimal: 3.333333333333333333333333333
- Double: 3.3333333333335
Hints & tips
- Choose Float, Decimal, or Double based on how specific you want your decimal answer to be.
- 00:03 Okay, in this video I want to talk about integers versus floats versus decimals
- 00:07 versus doubles, and we've sort of already touched on this a little bit when we
- 00:11 talked about the division stuff a couple of videos ago.
- 00:14 So let's start out by creating some integers, so
- 00:19 let's go int x equals 10 and let's go int y equals 3.
- 00:24 So let's come down here and let's go x divided by y.
- 00:28 So here, 10 divided by three is 3.333333.
- 00:32 What do we think the answer is going to be here?
- 00:36 It can only return whole number integers.
- 00:38 So we get what we would expect three, so it rounds down 3.33333.
- 00:43 0.3 is less than 0.5 so
- 00:46 you would expect it to round down, and the answer is here 3.
- 00:50 Okay, that's what we sort of expect.
- 00:52 But what happens if we do a float for each of these?
- 00:58 And remember, we stick our little f here.
- 01:01 So now, this is going to be a floating point.
- 01:04 So let me just sort of go float and
- 01:08 then let's also do decimal and also double.
- 01:15 So let's see what we get for the answer for
- 01:17 each of these using each of these different data types.
- 01:20 So here we have float.
- 01:22 Let's go ahead and run this.
- 01:23 We get 3.33333.
- 01:25 So there we go, 3.33333.
- 01:27 So what about decimal?
- 01:30 Well, let's change this to decimal and see, so that should be an m.
- 01:34 Remember decimals, and we get 3.33333, much more threes.
- 01:39 So let's go ahead and copy this, stick this in there.
- 01:43 And let me just sort of tab this over so we can compare these more.
- 01:47 And finally we have double, so here let's go double and this is going to be d.
- 01:52 We save this and run it.
- 01:53 We have 3.33335, which is slightly different.
- 01:58 So very different answers, and let's put int in here just for
- 02:02 fun, based on whether you have integers, floats, decimals, and doubles.
- 02:07 Now what happens if we change these to ten point something?
- 02:12 Will this change anything?
- 02:14 Nope, not there.
- 02:16 What about if we change it to decimal, And change this to M?
- 02:25 Save this and run it.
- 02:28 Nope, we still get that big long thing.
- 02:31 And just for good measure, let's go float.
- 02:39 Change this.
- 02:42 Same answer there, it's there, very interesting.
- 02:46 So just sort of keep in mind, if you're using very precise things where
- 02:52 you need to go out to have many decimal places, decimals looks like the way to go.
- 02:58 Double is better than float, it's going to sort of round the last thing.
- 03:02 Float will give you the least of all of them, it seems like.
- 03:06 An integer will just return an integer as we would expect, and
- 03:09 that's all there is to it.
- 03:11 So that's all for this video.
- 03:12 In the next video, we'll look at increment and decrement operators.
Lesson notes are only available for subscribers.