1 00:00:00,180 --> 00:00:02,890 Here we have some code that assigns the integer 12 to 2 00:00:02,890 --> 00:00:04,960 a variable named wholeNumber. 3 00:00:04,960 --> 00:00:10,930 And the fractionalNumber, 12.34, to a variable named fractionalNumber. 4 00:00:10,930 --> 00:00:15,450 That decimal point within the numbers is important because it decides which 5 00:00:15,450 --> 00:00:19,340 of C sharp's two most common numeric types the number will be treated as. 6 00:00:20,422 --> 00:00:23,710 The wholeNumber automatically gets assigned the type of Int32, and 7 00:00:23,710 --> 00:00:27,840 the number with the decimal point gets a type of Double. 8 00:00:27,840 --> 00:00:31,550 As we saw in the previous video, even a number that's technically and 9 00:00:31,550 --> 00:00:35,890 integer will be treated as a double, if you include a decimal point. 10 00:00:35,890 --> 00:00:41,530 Let's change the assignment to wholeNumber from 12 to 12.0. 11 00:00:41,530 --> 00:00:43,590 It's still, technically, the same number, but 12 00:00:43,590 --> 00:00:46,120 the decimal point changes its default type. 13 00:00:47,530 --> 00:00:50,850 You can see that the type for wholeNumber is now also Double. 14 00:00:52,710 --> 00:00:56,850 Remember that the var keyword implicitly sets the type of a variable 15 00:00:56,850 --> 00:00:59,330 based on the value you initially set it to. 16 00:01:00,500 --> 00:01:04,010 We can also explicitly set the type of our variables. 17 00:01:04,010 --> 00:01:07,760 Let me set wholeNumber's type to int, and 18 00:01:07,760 --> 00:01:12,800 fractionalNumber's type to double, save that and run it. 19 00:01:14,770 --> 00:01:17,615 But this gives us an error when assigning to wholeNumber. 20 00:01:18,680 --> 00:01:23,360 Cannot implicitly convert type 'double' to 'int', what does that mean? 21 00:01:23,360 --> 00:01:27,846 Well, this really doesn't seem to matter much when assigning 12.0 to 22 00:01:27,846 --> 00:01:29,208 an integer variable. 23 00:01:29,208 --> 00:01:32,859 But what if we were trying to assign 12.5 or 3.14, or 24 00:01:32,859 --> 00:01:35,940 some other number with a fractional part? 25 00:01:35,940 --> 00:01:38,820 For an integer variable to hold the double value, 26 00:01:38,820 --> 00:01:41,420 we have to convert it to an integer. 27 00:01:41,420 --> 00:01:45,440 But what do we do with the fractional part, the 0.75? 28 00:01:45,440 --> 00:01:47,910 Does it just not matter, can we drop it? 29 00:01:47,910 --> 00:01:51,960 That seems unlikely, you wouldn't want the fractional dollars dropped from 30 00:01:51,960 --> 00:01:53,670 your bank account, for example. 31 00:01:53,670 --> 00:01:58,340 To protect you from issues like this, C sharp won't let you assign a double value 32 00:01:58,340 --> 00:02:01,450 to a int variable without converting it first. 33 00:02:01,450 --> 00:02:05,460 A conversion shows the compiler that you know this is a double value, but 34 00:02:05,460 --> 00:02:07,990 you definitely want to convert it to an int. 35 00:02:07,990 --> 00:02:12,010 It shows you are certain there’s either no fractional portion to the number, or 36 00:02:12,010 --> 00:02:15,590 that losing the fractional portion won’t cause any problems. 37 00:02:15,590 --> 00:02:20,420 You do a conversion by placing parentheses before the value you want to convert, 38 00:02:20,420 --> 00:02:23,640 containing the type you want to convert the value to. 39 00:02:23,640 --> 00:02:28,920 So in this case, we want to convert this double value, 12.75, to an integer. 40 00:02:29,970 --> 00:02:34,540 Let's try saving this and see if it runs now. 41 00:02:34,540 --> 00:02:39,404 Everything works again, this value, 12.75, which is the double by default, 42 00:02:39,404 --> 00:02:41,102 has been converted to an int. 43 00:02:44,401 --> 00:02:47,600 Conversions only work with numeric types. 44 00:02:47,600 --> 00:02:50,670 For example, you can't convert from a string to an int, and 45 00:02:50,670 --> 00:02:52,740 you can't convert from an int to a string. 46 00:02:54,150 --> 00:02:56,490 If I try, I get compile errors. 47 00:02:56,490 --> 00:03:00,730 There are often other ways to convert from one non-numeric type to another, but 48 00:03:00,730 --> 00:03:03,320 those are provided by libraries. 49 00:03:03,320 --> 00:03:07,420 Conversions, on the other hand, are provided by the language itself. 50 00:03:07,420 --> 00:03:11,810 We'll see how to convert from a string to a numeric type in an upcoming video. 51 00:03:11,810 --> 00:03:16,640 Casting is an explicit conversion, it's required when making a conversion from 52 00:03:16,640 --> 00:03:21,500 a more precise numeric type to a less precise one because data might be lost. 53 00:03:22,640 --> 00:03:27,350 Converting from a floating-point number to an integer loses all the decimal places, 54 00:03:27,350 --> 00:03:31,370 for example, so it requires an explicit cast. 55 00:03:31,370 --> 00:03:35,200 But when you're converting from a less precise numeric tied to a more precise 56 00:03:35,200 --> 00:03:37,265 one, there's no risk of data loss. 57 00:03:37,265 --> 00:03:38,565 Because, the most precise, 58 00:03:38,565 --> 00:03:43,645 numeric type will always be able to hold the full value from the less precise one. 59 00:03:43,645 --> 00:03:47,655 For example, when converting from an integer to a floating-point number, 60 00:03:47,655 --> 00:03:50,935 you can just add a 0.0 onto the end. 61 00:03:50,935 --> 00:03:55,320 It consumes more memory to store it now, but it's still the same value. 62 00:03:55,320 --> 00:03:59,180 When converting from a less precise numeric type to a more precise one, 63 00:03:59,180 --> 00:04:01,995 C sharp will do an implicit cast. 64 00:04:01,995 --> 00:04:06,810 An implicit cast is a conversion between numeric types that happens automatically 65 00:04:06,810 --> 00:04:08,775 without the need to specify that it should. 66 00:04:09,905 --> 00:04:14,165 For example, suppose I wanted to change the initial value of fractional number 67 00:04:14,165 --> 00:04:17,415 from 12.34 to just 12. 68 00:04:17,415 --> 00:04:21,617 There's no need to write 12.0, 69 00:04:21,617 --> 00:04:25,707 I can just get rid of the decimal point and all the decimal places. 70 00:04:25,707 --> 00:04:29,537 It still works and the resulting value still has a type of double 71 00:04:29,537 --> 00:04:33,017 because I declared fractional number with a double type. 72 00:04:33,017 --> 00:04:37,367 C sharp did an implicit conversion from the less precise int type 73 00:04:37,367 --> 00:04:39,167 to the more precise double type.