1 00:00:00,450 --> 00:00:03,210 You can use the double tape to store decimal numbers. 2 00:00:05,420 --> 00:00:08,790 So far, we used the end long variable to store whole numbers. 3 00:00:09,320 --> 00:00:14,480 I want to emphasize the word hole because you cannot store decimal values with Ent. or along. 4 00:00:15,110 --> 00:00:20,750 The entire variable is used to store small hole numbers and the long variable can store big hole numbers. 5 00:00:22,170 --> 00:00:25,830 So in this video, we're going to use the double tape to store decimal numbers. 6 00:00:27,700 --> 00:00:32,650 First thing I'll need you to do is create a new class by yourself inside the section to project create 7 00:00:32,650 --> 00:00:36,720 a new file named Decimals Java and inside the decimals class. 8 00:00:36,730 --> 00:00:38,440 Make sure it has the main method. 9 00:00:45,040 --> 00:00:51,700 You can store decimals in a double variable, you can't use Int or long to store decimals, although 10 00:00:51,700 --> 00:00:53,200 we can do so in a double. 11 00:00:54,720 --> 00:00:57,370 And there are three things you need to right to define a double. 12 00:00:57,990 --> 00:01:03,600 You have to specify the double type, which says that this variable is meant to store decimals, the 13 00:01:03,600 --> 00:01:08,520 variable name, which in this case is price and the decimal value that you want to store. 14 00:01:09,300 --> 00:01:13,740 This code creates a variable called price, which stores a decimal value of three point ninety nine. 15 00:01:15,970 --> 00:01:20,110 So when the decimals class, we're going to create a variable named percentage double. 16 00:01:21,500 --> 00:01:22,580 Percentage. 17 00:01:24,770 --> 00:01:28,340 Is equal to fifty five point seven. 18 00:01:30,310 --> 00:01:39,190 Now we're going to print this percentage inside a string system, dart out dot print line will say in 19 00:01:39,190 --> 00:01:40,120 2020. 20 00:01:44,080 --> 00:01:50,890 Disconnect the string, embed, the percentage will say in 2020, some percentage. 21 00:01:56,390 --> 00:01:58,490 Of the world's population is urban. 22 00:02:05,530 --> 00:02:07,750 OK, compile your code and run it. 23 00:02:16,720 --> 00:02:17,300 Looks good. 24 00:02:18,020 --> 00:02:23,180 The variable percentage stores the decimal fifty five point seven, and you printed the value from within 25 00:02:23,180 --> 00:02:23,850 a string. 26 00:02:24,170 --> 00:02:25,190 Easy stuff. 27 00:02:27,970 --> 00:02:33,790 Back to our quote, I'd like to emphasize that you cannot store in decimals inside an Int or long variable. 28 00:02:34,390 --> 00:02:36,100 If you try to do it, you'll get an error. 29 00:02:37,390 --> 00:02:39,720 Let's try to change percentage to Annette. 30 00:02:41,160 --> 00:02:42,290 Compile your code. 31 00:02:45,150 --> 00:02:51,510 And we get an error in this line of our code and variables can only store whole numbers, so change 32 00:02:51,510 --> 00:02:52,950 the variable back to a double. 33 00:02:56,890 --> 00:02:58,300 And clear the output. 34 00:03:00,630 --> 00:03:04,830 Now, what happens if we reverse this and try to store a whole number in a double variable? 35 00:03:05,250 --> 00:03:07,500 Let's find out double. 36 00:03:08,470 --> 00:03:13,390 Dividend, we're going to create a double variable dividend and set it equal to 25. 37 00:03:16,450 --> 00:03:22,180 And we'll print it system dot out, dot print line dividend. 38 00:03:27,480 --> 00:03:29,880 Compiling our code and running it. 39 00:03:33,660 --> 00:03:39,180 And it prints it as a decimal double variables are special because even if you don't express the number 40 00:03:39,180 --> 00:03:41,780 as a decimal double, it's going to store it as one. 41 00:03:42,660 --> 00:03:45,930 In this case, the dividend variable stores the value twenty five. 42 00:03:46,620 --> 00:03:51,280 But since dividend is a double, it stores it as a decimal 25 point zero. 43 00:03:52,650 --> 00:03:58,800 With that being said, it's imperative that you always use double for math calculations if precision 44 00:03:58,800 --> 00:03:59,470 is important. 45 00:03:59,490 --> 00:04:00,780 You need to have decimals. 46 00:04:02,880 --> 00:04:06,450 So create a variable named deviser double divisor. 47 00:04:08,680 --> 00:04:10,270 And set it equal to two. 48 00:04:11,970 --> 00:04:17,490 Then we can divide both numbers using the division symbol, the front slash, so inside the Prince Mint 49 00:04:17,519 --> 00:04:20,190 will print the result of dividing both numbers. 50 00:04:27,260 --> 00:04:33,860 And as expected, 25 divided by two is twelve point five and dividend is a double edged sword. 51 00:04:33,890 --> 00:04:40,790 25 as a decimal, 25 point zero, the divisor double variable stores, two as a decimal 2.0. 52 00:04:41,450 --> 00:04:44,960 And when you divide a decimal by another, the result is a decimal value. 53 00:04:48,140 --> 00:04:53,420 All right, with that being said, avoid using int or long variables for math calculations. 54 00:04:54,260 --> 00:04:57,590 If you divide two whole numbers, the result will always be a whole number. 55 00:04:59,240 --> 00:05:05,600 This may not seem like an issue, but make dividend and deviser integer variables instead into dividend 56 00:05:05,600 --> 00:05:06,500 and divisor. 57 00:05:12,620 --> 00:05:13,700 Now, run that. 58 00:05:17,800 --> 00:05:23,620 And I hope you can see that the result is not correct, 25 divided by two is twelve point five, not 59 00:05:23,620 --> 00:05:24,010 12. 60 00:05:26,420 --> 00:05:32,060 The dividend variable stores, a whole number 25, the divisor variable also stores a whole number, 61 00:05:32,510 --> 00:05:35,960 and if you divide two integers, the result is going to be an integer. 62 00:05:36,830 --> 00:05:38,850 Int isn't able to work with decimals. 63 00:05:38,870 --> 00:05:45,280 So what it does is it cuts off decimals from the results and keeps only the integer in math. 64 00:05:45,290 --> 00:05:47,410 You need decimals to stay accurate. 65 00:05:47,720 --> 00:05:53,810 So always use double variables in math operations because double treats every number as a decimal and 66 00:05:53,810 --> 00:05:55,040 keeps the math accurate. 67 00:05:56,710 --> 00:05:58,540 So going to switch this back to double's. 68 00:06:05,060 --> 00:06:09,710 All right, finally, we could make the print statements more interesting to end off on a high note. 69 00:06:09,740 --> 00:06:12,560 So what we're going to do is embed this result in a string as well. 70 00:06:13,460 --> 00:06:15,770 We're going to print the dividend disvalue. 71 00:06:16,910 --> 00:06:19,550 Divided by break the string. 72 00:06:21,200 --> 00:06:22,610 The divisor value. 73 00:06:23,640 --> 00:06:24,990 Is equal to. 74 00:06:28,110 --> 00:06:31,980 The arithmetic result of dividing dividend by divisor. 75 00:06:38,970 --> 00:06:40,020 Run the code. 76 00:06:46,290 --> 00:06:51,810 OK, let's add some space between the word dividend and the first string recompiling the code. 77 00:06:57,110 --> 00:07:03,680 We can wrap this lesson up with these rules, use into historical numbers, use long to store very large 78 00:07:03,680 --> 00:07:07,820 hole numbers and use double to store and work with decimals. 79 00:07:10,410 --> 00:07:15,180 And this lesson you learn to use double to sword decimal values, you created a double variable that 80 00:07:15,180 --> 00:07:19,120 stores a percentage and then you printed the value for more than a string. 81 00:07:19,710 --> 00:07:20,630 That was pretty easy. 82 00:07:23,480 --> 00:07:29,240 And double stories, any number as a decimal, even if you don't express it as one and decimals help 83 00:07:29,240 --> 00:07:34,880 keep the math accurate, that's why you should always use double for math, avoid using whole numbers 84 00:07:34,880 --> 00:07:39,860 in math calculations, because if you divide two integer values, the result is going to be an integer 85 00:07:39,860 --> 00:07:40,270 value. 86 00:07:40,760 --> 00:07:44,640 It cuts off the decimal and keeps only the integer, which is not correct. 87 00:07:45,500 --> 00:07:52,460 So to avoid mistakes, no to use int to store whole numbers long, to store very large hole numbers 88 00:07:52,790 --> 00:07:55,490 and double to store and work with decimals.