1 00:00:00,690 --> 00:00:01,530 ‫Welcome back. 2 00:00:01,560 --> 00:00:06,270 ‫In this video, we are going to have a look at conversion, which means we're going to have a look at 3 00:00:06,270 --> 00:00:08,400 ‫implicit and explicit conversion. 4 00:00:08,400 --> 00:00:11,310 ‫So let's have a look at explicit conversion first. 5 00:00:11,310 --> 00:00:21,300 ‫Therefore, I create a double variable, which I call my double, and I'm going to assign a value of 6 00:00:21,300 --> 00:00:30,960 ‫one 1337, for example, and now I'm going to create an integer int my int and this is just going to 7 00:00:30,960 --> 00:00:33,090 ‫be nothing. 8 00:00:33,090 --> 00:00:35,220 ‫So for now I don't initialize it. 9 00:00:35,220 --> 00:00:37,680 ‫So my end doesn't have a value yet. 10 00:00:37,890 --> 00:00:46,500 ‫And if I want to put my double value into my integer and cast it into an integer, so let's do that 11 00:00:46,500 --> 00:00:58,200 ‫cast double into int or to int, which now will result in that value being stored as an integer. 12 00:00:58,200 --> 00:01:04,980 ‫But as you know, integers can only have whole numbers, which means that those additional decimals 13 00:01:04,980 --> 00:01:06,210 ‫here will be gone. 14 00:01:06,360 --> 00:01:08,370 ‫So let's just have a look at casting. 15 00:01:08,370 --> 00:01:15,540 ‫So my int is going to be an integer from my double. 16 00:01:15,990 --> 00:01:20,940 ‫So what I do here is I say please make an integer out of the double. 17 00:01:20,970 --> 00:01:23,160 ‫And that's what I do with this casting. 18 00:01:23,160 --> 00:01:26,400 ‫So that's explicit conversion. 19 00:01:27,120 --> 00:01:31,080 ‫So now let's have a look at how this looks like on the console. 20 00:01:31,110 --> 00:01:41,070 ‫So right line and I'm going to write my int and finally console.log read. 21 00:01:41,820 --> 00:01:51,810 ‫All right so now let's run that and there we are 13 is printed out and as you can see, it's not 13.37, 22 00:01:51,810 --> 00:01:53,010 ‫it's 13. 23 00:01:53,010 --> 00:02:02,370 ‫That's because it can only contain whole numbers and it just cut off anything behind the decimal point, 24 00:02:02,370 --> 00:02:09,600 ‫which means that no matter what, what's stood here, so even if it's 13.9, seven or something like 25 00:02:09,600 --> 00:02:12,900 ‫that, it doesn't round the value down or up. 26 00:02:12,900 --> 00:02:13,560 ‫It doesn't care. 27 00:02:13,560 --> 00:02:15,960 ‫It just cuts it off after the decimal point. 28 00:02:16,740 --> 00:02:17,070 ‫All right. 29 00:02:17,070 --> 00:02:19,830 ‫So that's explicit conversion. 30 00:02:21,570 --> 00:02:24,930 ‫Explicit conversion. 31 00:02:25,170 --> 00:02:28,190 ‫And now let's have a look at implicit conversion. 32 00:02:28,200 --> 00:02:31,800 ‫So let's put it up here. 33 00:02:33,450 --> 00:02:43,290 ‫Implicit conversion, which is, for example, I have a number of type integer and it's a well it's 34 00:02:43,290 --> 00:02:44,160 ‫any number. 35 00:02:44,160 --> 00:02:49,440 ‫And now I want to store it into my big number. 36 00:02:49,440 --> 00:02:52,830 ‫So I'm going to create a very big number and that's just going to be number. 37 00:02:53,370 --> 00:02:54,360 ‫So you can do that. 38 00:02:54,360 --> 00:02:58,650 ‫You can simply assign a smaller type value. 39 00:02:58,650 --> 00:03:07,650 ‫So this means you can simply assign a smaller value of type int into a long you could assign. 40 00:03:08,940 --> 00:03:11,730 ‫For example, let's create a float. 41 00:03:12,600 --> 00:03:26,670 ‫My float is 13.37 and my new double is going to be my float. 42 00:03:27,300 --> 00:03:35,400 ‫So you can do that either with an F or a capital F, you can do that so either from long to int or from 43 00:03:35,400 --> 00:03:40,290 ‫double to float, or from other bigger types to smaller types, pretty much. 44 00:03:41,760 --> 00:03:45,300 ‫So that's implicit and explicit conversion. 45 00:03:45,300 --> 00:03:48,830 ‫And there is something else which is type conversion. 46 00:03:48,840 --> 00:03:51,720 ‫So what you also can do is the following. 47 00:03:53,700 --> 00:03:55,740 ‫So type conversion. 48 00:03:56,220 --> 00:04:01,340 ‫For example, if I want to convert a double type to a string type. 49 00:04:01,350 --> 00:04:08,340 ‫So in our case, right line can handle doubles integers, it can handle strings and so forth. 50 00:04:08,340 --> 00:04:12,150 ‫But in some other cases you actually need to have a string. 51 00:04:12,150 --> 00:04:16,830 ‫So it needs to be a string, for example, to be handled by the method. 52 00:04:16,830 --> 00:04:26,280 ‫So in order to make a string out of a double or an integer, you can simply use conversions. 53 00:04:26,280 --> 00:04:28,050 ‫And now let's have a look at one. 54 00:04:28,050 --> 00:04:31,290 ‫I'm just going to call that string. 55 00:04:31,290 --> 00:04:42,240 ‫My string is equal to and now I use my double, which we know is a double value and now I convert it 56 00:04:42,240 --> 00:04:49,290 ‫into a string so converts the numeric value of this instance to its equivalent string representation. 57 00:04:49,290 --> 00:05:04,920 ‫So what that will do is it will simply convert 13.37 to something like 13.37, but in quotation marks. 58 00:05:05,990 --> 00:05:06,380 ‫All right. 59 00:05:06,380 --> 00:05:08,060 ‫So that's what we can do. 60 00:05:08,060 --> 00:05:09,590 ‫And we can do that with doubles. 61 00:05:09,590 --> 00:05:11,930 ‫We can do that with integers. 62 00:05:11,930 --> 00:05:22,940 ‫So let's go ahead and use num dot to string to string so you can do that or you could do the same thing 63 00:05:22,940 --> 00:05:24,470 ‫with longs and so forth. 64 00:05:24,470 --> 00:05:24,980 ‫All right. 65 00:05:24,980 --> 00:05:27,500 ‫So pretty much the same principle. 66 00:05:27,710 --> 00:05:29,270 ‫Now a little challenge for you. 67 00:05:29,270 --> 00:05:32,540 ‫Please go ahead and convert. 68 00:05:33,650 --> 00:05:38,210 ‫The float might float into a string as well. 69 00:05:41,060 --> 00:05:41,420 ‫All right. 70 00:05:41,420 --> 00:05:42,510 ‫I hope you tried it. 71 00:05:42,530 --> 00:05:50,060 ‫My float string is equal to my float to string. 72 00:05:50,570 --> 00:05:50,920 ‫All right, then. 73 00:05:50,930 --> 00:05:56,000 ‫Now let's print it out just to see what that does to my float string. 74 00:05:56,120 --> 00:05:56,990 ‫Let's run it. 75 00:05:58,290 --> 00:06:00,510 ‫And we get 13.37. 76 00:06:03,460 --> 00:06:06,460 ‫Now you can do that even with Booleans. 77 00:06:06,460 --> 00:06:11,020 ‫And we haven't covered booleans yet, but booleans are working as follows. 78 00:06:11,020 --> 00:06:16,510 ‫So let's have a look at bool and simply create one here. 79 00:06:18,100 --> 00:06:21,220 ‫Bool sun is shining. 80 00:06:21,220 --> 00:06:25,120 ‫So this is just a variable which can only have two values. 81 00:06:25,120 --> 00:06:29,920 ‫So it can either be true or it can be false. 82 00:06:29,920 --> 00:06:34,390 ‫So now please go ahead and look outside and see if the sun is shining. 83 00:06:34,390 --> 00:06:38,950 ‫And if the sun is shining, then enter true and otherwise enter false. 84 00:06:39,520 --> 00:06:47,230 ‫And now what you can do is you can convert the bool sun is shining to a string as well. 85 00:06:47,680 --> 00:06:55,090 ‫So you can simply do that and I'm going to save that in string. 86 00:06:55,090 --> 00:07:00,040 ‫My bool string is equal to sun shining to string. 87 00:07:00,040 --> 00:07:06,550 ‫So now if this variable here and it's called my bool string, so I'm just going to go ahead and call 88 00:07:06,550 --> 00:07:10,210 ‫that my bool string and see what it says here. 89 00:07:10,210 --> 00:07:11,770 ‫And it says false. 90 00:07:11,920 --> 00:07:14,290 ‫As you can see, it should simply print it out. 91 00:07:14,290 --> 00:07:14,920 ‫False. 92 00:07:14,920 --> 00:07:19,600 ‫So unfortunately, the sun is not shining here in Cologne, Germany, right now. 93 00:07:20,990 --> 00:07:21,470 ‫All right. 94 00:07:21,470 --> 00:07:28,730 ‫So once again, real quick, we have implicit conversion, which means we convert from one bigger type 95 00:07:28,730 --> 00:07:31,790 ‫to a smaller type, which works for numbers. 96 00:07:31,910 --> 00:07:39,980 ‫Then we have the explicit conversion which casts, for example, a double to an integer or which can 97 00:07:39,980 --> 00:07:44,120 ‫cause a double to a big number or something like that. 98 00:07:44,120 --> 00:07:53,060 ‫So to a long then we have type conversion which converts, for example, a double to a string or a flow 99 00:07:53,060 --> 00:07:56,390 ‫to a string or any number type to a string. 100 00:07:57,200 --> 00:08:03,290 ‫And in the next video, we are going to have a look at parsing where you can see how to parse from a 101 00:08:03,290 --> 00:08:07,880 ‫string to a data type, which is a number. 102 00:08:07,940 --> 00:08:10,400 ‫So for example, a double, a float, an integer. 103 00:08:10,400 --> 00:08:15,320 ‫And that's something that is highly relevant when you, for example, read out information from the 104 00:08:15,320 --> 00:08:22,280 ‫user because usually you get this information in sort of a string and you have to convert it into a 105 00:08:22,280 --> 00:08:27,440 ‫double or an integer in order to calculate with it or something similar. 106 00:08:27,770 --> 00:08:28,160 ‫Great. 107 00:08:28,160 --> 00:08:29,480 ‫So see you in the next video. 108 00:08:29,520 --> 00:08:30,620 ‫We're going to cover that.