Locked lesson.
About this lesson
In this lesson, we learn about Math Methods such as Floor, Ceiling, Round, and Truncate.
Exercise files
Download this lesson’s related exercise files.
18 - Math Methods.docx61.1 KB 18 - Math Methods SOLUTION.docx
57.9 KB
Quick reference
Math Methods
C# has a number of built in Math Methods.
When to use
Use these as needed whenever you want to do certain math functions.
Instructions
double x = 5.2d;
Math.Floor(x); // find the lower level floor
Math.Ceiling(x); // find the upper level ceiling
Math.Round(x); // Rounding the number
Math.Round(x, 2); // Round to 2 decimal places
Math.Truncate(x); // chop off decimal places
Hints & tips
- Math methods are a quick and easy way to manipulate your numbers with C#.
- 00:04 Okay, in this video I want to talk about different math functions.
- 00:06 And there are lots of different math functions that are built into C sharp.
- 00:09 In this video, we're just going to talk about three or four,
- 00:11 just to give you a brief idea of some of the things that are available.
- 00:14 And as you learn more C sharp in the future, you can go out and
- 00:16 find other math functions, they are pretty easy to look up.
- 00:18 So a math function is just what it sounds like.
- 00:21 It allows us to do some sort of math thing to some numbers.
- 00:25 So I'm going to create a double, and let's just call it x and set it equal to 5.2.
- 00:31 And then let's pull this up here, and let's say your number is,
- 00:39 and then let's concatenate x on there.
- 00:43 Right, so the first math function I want to look at is floor.
- 00:46 And what this will do, is it will change your number to its lower level, its floor.
- 00:52 So it's sort of like rounding.
- 00:54 It will take this and bring it down.
- 00:57 So let's go ahead and copy this and see this thing in action.
- 01:02 And here I'm just going to type floor, so we can keep track of this.
- 01:07 And then to call this we just call math.floor(), and
- 01:10 then pass in whatever we want to use the function on.
- 01:14 In this case, our variable x.
- 01:16 So if we go ahead and save this, let's see, we need another parentheses,
- 01:20 there we go.
- 01:20 We go ahead and save this, run this, we see our number is 5.2.
- 01:26 The floor is 5.
- 01:27 It's the lower limit of that thing.
- 01:29 We can change this to something higher like 5.8.
- 01:33 If we save this and run it again, we're still just going to get 5,
- 01:37 because it's lowering it to whatever the floor is, the lower level of it is.
- 01:41 So again, that's kind of cool.
- 01:43 The next one we want to look at is ceiling.
- 01:46 And so here, let's just copy this guy, and type in ceiling.
- 01:52 Same thing, instead math.floor(), it's math.ceiling().
- 01:55 And this is how you call all of these functions,
- 01:58 math dot whatever the function is.
- 01:59 And in fact you can kind of come over here and type in math dot, and
- 02:03 then you can get a list of a lot of these that you could play around with.
- 02:07 So I recommend playing around with all of these when you get some time,
- 02:10 just click on them and see what they do.
- 02:12 Right, so here, math.ceiling(), let's change this.
- 02:16 If we save this and run it, We see our number is 5.8.
- 02:21 The ceiling is 6, it rounds up to the higher level,
- 02:25 the highest point that your number can be, right?
- 02:28 So that's kind of cool.
- 02:29 The next one is actual rounding, right?
- 02:33 So these first two aren't really making a rounding determination.
- 02:36 They're just going up and down, to the highest level and the lowest level.
- 02:39 That's not technically rounding.
- 02:41 If we wanted to round, we could do it like this.
- 02:44 And we could call around.
- 02:46 Now, if we around this guy we see our number is 5.8.
- 02:51 If it rounds, anything above 5.5, we'll round up.
- 02:54 So it changed it to 6.
- 02:56 If we changed our number back to 5.3 or 5.2, something below 5.5 and
- 03:03 run this again, remember back rounding in our school days?
- 03:08 It will round down, and now our number becomes 5.
- 03:12 So very interesting.
- 03:13 Now we could tell this how many decimal places we want to round.
- 03:17 So if we want to say one, well, let's change this to 3.3.
- 03:22 So now it's going to take this 3 right here,
- 03:26 and it will round it based on what's here.
- 03:29 So this will be 5.3.
- 03:31 So if we save this and run it, we see sure enough, 5.3.
- 03:37 If we change this to 8, now that is above 5, so
- 03:42 it should round it up to 5.4, right?
- 03:45 So if we save this and run it, We see sure enough 5.4.
- 03:52 So you can be very specific in how many decimal places you want.
- 03:56 Just change this second number right here.
- 03:58 So that's cool.
- 04:00 Finally, I want to look at truncate.
- 04:02 And this will just sort of chop off the decimals.
- 04:05 So, again, we could do this, and let's go, truncate.
- 04:10 And here instead of ceiling, it's as you guess, truncate.
- 04:14 If we save this and run it, We see,
- 04:20 boom, it's just chopped off all of our decimal points.
- 04:23 So like I said, those are just a few math functions.
- 04:25 If you want to learn more, you could just Google math functions, or
- 04:29 you could just type in math dot and you see we have a big list of them.
- 04:32 Here's truncate, log, round, ceiling, absolute value,
- 04:36 then scroll through, all kinds of different things.
- 04:39 Some of these are intuitive, some of them are not.
- 04:42 There's round, sine, square root, tangent, truncate again, all the things.
- 04:47 So, play around with this, very interesting,
- 04:50 and that's all there is to it.
- 04:52 So that's all for this video.
- 04:53 In the next video we'll look at converting integers to strings.
Lesson notes are only available for subscribers.