Locked lesson.
About this lesson
We examine the process for Type Conversion and Casting.
Exercise files
Download this lesson’s related exercise files.
20 - Type Conversion Casting.docx61 KB 20 - Type Conversion Casting SOLUTION.docx
58 KB
Quick reference
Type Conversion/Casting
Type casting allows you to convert different number data types into one another.
When to use
Use this whenever you need to convert one number data type into another.
Instructions
float floatNumber = 5.2f;
int intNumber;
Convert.ToInt32(floatNumber); // Rounds
intNumber = (int)floatNumber; // Doesn't round
Hints & tips
- Convert.ToInt32(floatNumber);
- intNumber = (int)floatNumber;
- 00:04 Okay, in the last video we looked at converting integers into strings.
- 00:08 In this video, I want to talk about type conversion and casting.
- 00:11 So basically we have different number data types that we want to convert into other
- 00:14 number of data types.
- 00:15 So we have floats, we have decimals, we have integers.
- 00:18 How do we convert things around from one of those to another?
- 00:21 That's what we're going to look at in this video.
- 00:23 And there's a couple of different ways that you can do it.
- 00:25 I'll talk about both of those ways and why one may or
- 00:28 may not be better than the other, and we'll look at all of that in this video.
- 00:32 So let's start out with a float.
- 00:35 And let's call this float number and set that as equal to, I don't know,
- 00:40 5.2 or something, remember our f on there.
- 00:44 And let's also create an int, and we'll call this int number and
- 00:48 set that equal to, I don't know, 10, something like that.
- 00:53 So or actually just to make it a little more easy to follow,
- 00:56 we don't have to assign anything to that.
- 00:59 So we've got a float, let's say we want to convert it into an integer and
- 01:03 assign it into this variable.
- 01:05 Well what we learned in the last video,
- 01:08 if we just go int number equals float number, and then come down here and
- 01:14 try and print it number, we're likely to get an error.
- 01:19 And we do sure enough, we look at our error here,
- 01:22 it says cannot implicitly convert type float to int.
- 01:26 An explicit conversion exists, you're missing a cast.
- 01:29 So we need to cast and convert this thing, right.
- 01:32 So we could do that, so we can use the convert method here.
- 01:36 And what do we want to convert it to?
- 01:38 Well we want to convert our float number to an integer.
- 01:41 So what is an integer?
- 01:42 It's this 2 int 32 guy, and then we just pass in the thing we want to convert.
- 01:49 How do I know that an integer is an int 32?
- 01:52 Well we can come over here and hover over this int right here, and
- 01:57 you can see in green it says int 32, so system.int 32.
- 02:01 We can do the same thing for float, and it says system.single, right.
- 02:05 So okay, that should work.
- 02:07 Let's go ahead and save this and run it, And we see we get 5.
- 02:14 Now our original number was 5.2, and
- 02:16 it has converted it into an integer and rounded down.
- 02:20 So what happens if we change our number here to something above 5.5,
- 02:24 like 5.7 for instance?
- 02:26 If we save this and run it, We get six, it rounds up.
- 02:32 So that's the nice thing about this method, this convert method,
- 02:37 it will actually round.
- 02:38 Like I said, there's another way you could do it.
- 02:40 You can just go int number equals and then use parentheses and
- 02:46 just pass in int, and then the number you want to convert, float number, right.
- 02:53 So if we save this and run it, remember our float number is
- 02:58 5.7, It changes it to 5, right.
- 03:03 You would think it would change it to six it would round up, but it doesn't.
- 03:06 It just takes the base number,, whatever is on this side of the decimal point, and
- 03:11 returns that.
- 03:12 So we can test this, we could change it to 5.2.
- 03:15 If we save this and ran it again, it's still just going to change it to 5.
- 03:22 So this convert is something you can use to convert any of the different things,
- 03:26 floats, decimals, doubles, anything you want.
- 03:29 And you can see when we type in convert dot, you can get a list
- 03:34 of things that you can convert, base 64 string, 2 int 32.
- 03:40 And you could scroll through here and see all the different things, bytes,
- 03:44 characters, date, time, booleans, all the different data types that you can convert,
- 03:49 int 16, int 64, and on and on.
- 03:51 But play around with this on your own and mess around with all of those things and
- 03:54 see what you can do it, and that's all there is to it.
- 03:56 So that's all for this video and all for the math section.
- 03:59 In the next section, in the next video,
- 04:02 we're going to jump into user input and output.
Lesson notes are only available for subscribers.