1 00:00:04,710 --> 00:00:07,200 so we've now got our calculator working 2 00:00:07,200 --> 00:00:08,940 with the view model to separate the 3 00:00:08,940 --> 00:00:11,070 logic from the user interface that's 4 00:00:11,070 --> 00:00:12,990 given us a few advantages over the first 5 00:00:12,990 --> 00:00:14,670 version we don't have to worry about 6 00:00:14,670 --> 00:00:17,279 saving State for one thing the view 7 00:00:17,279 --> 00:00:19,230 model persists across configuration 8 00:00:19,230 --> 00:00:22,140 changes and doesn't lose its state when 9 00:00:22,140 --> 00:00:24,690 the activity is recreated it subscribes 10 00:00:24,690 --> 00:00:26,910 to the view model again and receives the 11 00:00:26,910 --> 00:00:29,550 latest values to display but our 12 00:00:29,550 --> 00:00:31,770 calculator works like a scientific 13 00:00:31,770 --> 00:00:34,250 calculator it's not quite as useful for 14 00:00:34,250 --> 00:00:36,989 financial calculations to see what I 15 00:00:36,989 --> 00:00:38,760 mean run the app and let's work out tax 16 00:00:38,760 --> 00:00:42,000 on a sale when the tax rate is 10% so 17 00:00:42,000 --> 00:00:44,910 I'm going to start the emulator as fast 18 00:00:44,910 --> 00:00:50,700 for this bit as always 19 00:00:50,700 --> 00:00:52,170 all right so there's our calculator on 20 00:00:52,170 --> 00:00:55,170 the screen so let's workout that tax to 21 00:00:55,170 --> 00:00:58,110 work out the tax on $56 we multiply by 22 00:00:58,110 --> 00:01:00,900 0.1 so let's try that in the calculator 23 00:01:00,900 --> 00:01:07,670 I'm going to type 56 multiplied by 0.1 24 00:01:07,670 --> 00:01:11,640 Eagles now for most scientific purposes 25 00:01:11,640 --> 00:01:14,460 that's accurate enough but it's not very 26 00:01:14,460 --> 00:01:16,610 good for financial calculations though 27 00:01:16,610 --> 00:01:18,450 accountants don't really like it when 28 00:01:18,450 --> 00:01:20,640 they see values like that but it can get 29 00:01:20,640 --> 00:01:22,860 more obscure than that so if we enter 30 00:01:22,860 --> 00:01:25,680 equals 56 equals to reset the value back 31 00:01:25,680 --> 00:01:29,700 to 56 we'll do that first and we're 32 00:01:29,700 --> 00:01:31,170 going to try it another calculation now 33 00:01:31,170 --> 00:01:32,990 this time we're going to multiply by 34 00:01:32,990 --> 00:01:37,440 0.01 so multiplied here Oh point zero 35 00:01:37,440 --> 00:01:40,770 one equals and you can see we let the 36 00:01:40,770 --> 00:01:43,080 value there of zero point five six 37 00:01:43,080 --> 00:01:45,119 so that looks right now we should be 38 00:01:45,119 --> 00:01:46,619 able to multiply that by a hundred to 39 00:01:46,619 --> 00:01:48,630 get our original value back of a 40 00:01:48,630 --> 00:01:53,399 multiplied by 100 equals so once again 41 00:01:53,399 --> 00:01:55,170 we've got a strange rounding error there 42 00:01:55,170 --> 00:01:58,340 the reason is that the decimal fraction 43 00:01:58,340 --> 00:02:03,060 0.01 is a recurring value in binary now 44 00:02:03,060 --> 00:02:04,080 I'm not going to go into the inner 45 00:02:04,080 --> 00:02:06,750 details of storing decimal numbers you 46 00:02:06,750 --> 00:02:09,750 can find out about I Triple E 754 to 47 00:02:09,750 --> 00:02:11,790 learn more now there is actually a 48 00:02:11,790 --> 00:02:15,150 Wikipedia article on I Triple E 75 for 49 00:02:15,150 --> 00:02:16,800 that describes the standard quite well 50 00:02:16,800 --> 00:02:18,480 I'll quickly bring this up on the screen 51 00:02:18,480 --> 00:02:20,100 and you can check this out at your 52 00:02:20,100 --> 00:02:24,750 leisure what we will do though is have a 53 00:02:24,750 --> 00:02:26,270 look at how we can make our calculations 54 00:02:26,270 --> 00:02:29,819 more suitable for financial work so 55 00:02:29,819 --> 00:02:33,060 we're go back to Android studio and Java 56 00:02:33,060 --> 00:02:35,730 has a big decimal class that solves 57 00:02:35,730 --> 00:02:37,830 these strange rounding errors so what we 58 00:02:37,830 --> 00:02:39,390 can do is create a version of our view 59 00:02:39,390 --> 00:02:41,849 model that uses big decimal instead of 60 00:02:41,849 --> 00:02:44,190 double and use that on our weapons did 61 00:02:44,190 --> 00:02:45,900 so I'm going to go ahead and do that and 62 00:02:45,900 --> 00:02:48,360 we'll start off by actually copying the 63 00:02:48,360 --> 00:02:50,730 calculator view model class and pasting 64 00:02:50,730 --> 00:02:52,980 it back into the cot the project so I'm 65 00:02:52,980 --> 00:02:55,770 gonna copy and paste you can see that 66 00:02:55,770 --> 00:02:57,420 IntelliJ has come up asked is what the 67 00:02:57,420 --> 00:02:59,250 new name is I'm going to call this one 68 00:02:59,250 --> 00:03:01,140 instead of calculator view model we'll 69 00:03:01,140 --> 00:03:04,080 call this one big decimal view model 70 00:03:04,080 --> 00:03:06,690 percent to that we've never got a big 71 00:03:06,690 --> 00:03:09,420 decimal view model class I think that's 72 00:03:09,420 --> 00:03:10,620 quite a descriptive name for what 73 00:03:10,620 --> 00:03:13,320 ultimately it's going to be now there 74 00:03:13,320 --> 00:03:14,850 aren't many changes to make it's quite a 75 00:03:14,850 --> 00:03:16,710 simple change in fact we're just going 76 00:03:16,710 --> 00:03:18,180 to be using the big decimal class 77 00:03:18,180 --> 00:03:20,460 instead of the double class and if 78 00:03:20,460 --> 00:03:22,380 you're offered a choice import from Java 79 00:03:22,380 --> 00:03:24,630 math well so what we need to do is make 80 00:03:24,630 --> 00:03:26,880 some changes firstly on line 15 we need 81 00:03:26,880 --> 00:03:28,920 to change the type from double there to 82 00:03:28,920 --> 00:03:31,050 a big decimal and you can see I'm 83 00:03:31,050 --> 00:03:32,990 selecting there the java dot math import 84 00:03:32,990 --> 00:03:34,920 i'll just take a copy of that because it 85 00:03:34,920 --> 00:03:36,060 going to be pasting that in various 86 00:03:36,060 --> 00:03:38,670 places we also need to make a change to 87 00:03:38,670 --> 00:03:42,390 line 19 the result should be I'll type 88 00:03:42,390 --> 00:03:44,130 big decimal now and no longer a type 89 00:03:44,130 --> 00:03:46,290 double and we have got some errors 90 00:03:46,290 --> 00:03:47,730 specifically we need to have a look in 91 00:03:47,730 --> 00:03:51,180 the operand pressed function first see 92 00:03:51,180 --> 00:03:52,470 at the moment we're using to double 93 00:03:52,470 --> 00:03:54,990 their course that should be value safe 94 00:03:54,990 --> 00:03:58,110 called dot to big decimal I'm gonna 95 00:03:58,110 --> 00:04:02,190 change that to big decimal I've got an 96 00:04:02,190 --> 00:04:04,230 error here which will fix shortly and 97 00:04:04,230 --> 00:04:05,490 then scrolling down to the neck press 98 00:04:05,490 --> 00:04:07,770 function we need to do a similar thing 99 00:04:07,770 --> 00:04:09,750 here we've got to double we in use here 100 00:04:09,750 --> 00:04:12,480 we need to change that to big decimal 101 00:04:12,480 --> 00:04:14,730 I'm gonna make that change to DBT small 102 00:04:14,730 --> 00:04:17,040 so we need to change this line here line 103 00:04:17,040 --> 00:04:20,579 59 because we can't multiply a bit a big 104 00:04:20,579 --> 00:04:22,560 decimal by double so that should be 105 00:04:22,560 --> 00:04:24,870 double values star equals and it should 106 00:04:24,870 --> 00:04:30,120 be big decimal dot value of then in 107 00:04:30,120 --> 00:04:31,350 parenthesis we're going to put them on 108 00:04:31,350 --> 00:04:34,680 as one so that's fixed up that fire 109 00:04:34,680 --> 00:04:36,840 function now I'm moving down now to our 110 00:04:36,840 --> 00:04:39,360 perform operation function will change 111 00:04:39,360 --> 00:04:41,310 the declaration from value from a double 112 00:04:41,310 --> 00:04:44,610 to a big decimal we need to change this 113 00:04:44,610 --> 00:04:47,640 test here on line 79 currently if a 114 00:04:47,640 --> 00:04:49,580 value equals equals is zero point zero 115 00:04:49,580 --> 00:04:53,930 need to change that to big decimal dot 116 00:04:53,930 --> 00:04:58,760 value of zero point zero in parentheses 117 00:04:58,760 --> 00:05:01,380 which fixes the error then on the next 118 00:05:01,380 --> 00:05:03,120 line need to handle the attempt to 119 00:05:03,120 --> 00:05:06,900 divide by zero it shouldn't be WLAN it 120 00:05:06,900 --> 00:05:11,960 should now be big decimal dot value of 121 00:05:11,960 --> 00:05:17,480 so in parentheses double 13a 122 00:05:17,480 --> 00:05:20,250 well that should be all the changes we 123 00:05:20,250 --> 00:05:21,330 need to make here there's no longer any 124 00:05:21,330 --> 00:05:23,850 errors in there so we now need to do is 125 00:05:23,850 --> 00:05:28,620 go back to our main activity class we 126 00:05:28,620 --> 00:05:31,950 need to change this line this view model 127 00:05:31,950 --> 00:05:33,570 which is currently using the calculator 128 00:05:33,570 --> 00:05:35,820 view model to use our bigdecimal view 129 00:05:35,820 --> 00:05:37,350 model so we're going to take a copy of 130 00:05:37,350 --> 00:05:39,620 that line and comment the first one out 131 00:05:39,620 --> 00:05:42,150 and we're going to change this now to 132 00:05:42,150 --> 00:05:44,610 use the incentive calculated view model 133 00:05:44,610 --> 00:05:47,490 bigdecimal view model and that's 134 00:05:47,490 --> 00:05:48,120 literally it 135 00:05:48,120 --> 00:05:49,950 it should now use the bigdecimal view 136 00:05:49,950 --> 00:05:52,110 model and be able to perform accurate 137 00:05:52,110 --> 00:05:58,460 financial calculations let's try that 138 00:05:58,460 --> 00:06:00,450 all right so we do the same resort we've 139 00:06:00,450 --> 00:06:03,590 done before we'll start off by typing 56 140 00:06:03,590 --> 00:06:09,660 multiplied by 0.1 equals this time we're 141 00:06:09,660 --> 00:06:11,550 getting the answer 5.6 without all the 142 00:06:11,550 --> 00:06:14,130 extra decimals so that's good the other 143 00:06:14,130 --> 00:06:17,310 example we used was was first to see the 144 00:06:17,310 --> 00:06:18,960 value back 256 we could do that with 145 00:06:18,960 --> 00:06:22,860 equals 56 equals and then we multiplied 146 00:06:22,860 --> 00:06:29,790 by 0.01 so multiplied by 0.01 equals to 147 00:06:29,790 --> 00:06:32,729 0.5 6 so that looks right multiplying 148 00:06:32,729 --> 00:06:34,560 that by a hundred we should get our 149 00:06:34,560 --> 00:06:39,530 original value back so times 100 equals 150 00:06:39,530 --> 00:06:41,729 this time it works we're getting fifty 151 00:06:41,729 --> 00:06:44,310 six point two zero zero with no rounding 152 00:06:44,310 --> 00:06:46,440 errors so switching back to main 153 00:06:46,440 --> 00:06:48,780 activity we could have a menu option to 154 00:06:48,780 --> 00:06:50,040 allow the user to choose between 155 00:06:50,040 --> 00:06:53,490 scientific and financial modes all we'd 156 00:06:53,490 --> 00:06:55,380 have to do is subscribe to the 157 00:06:55,380 --> 00:06:56,940 appropriate view model in the code and 158 00:06:56,940 --> 00:06:59,090 the calculators behavior would change 159 00:06:59,090 --> 00:07:01,290 you may wonder why we don't just leave 160 00:07:01,290 --> 00:07:03,419 it using big decimals and we certainly 161 00:07:03,419 --> 00:07:05,250 could do that for this simple app 162 00:07:05,250 --> 00:07:06,810 remember that we're learning about 163 00:07:06,810 --> 00:07:09,330 general techniques here you may develop 164 00:07:09,330 --> 00:07:11,520 this app further to include things like 165 00:07:11,520 --> 00:07:14,430 sine and cosines these wouldn't be much 166 00:07:14,430 --> 00:07:16,140 easier to an accountant and the 167 00:07:16,140 --> 00:07:18,000 bigdecimal class doesn't support them 168 00:07:18,000 --> 00:07:20,010 you can't find the cosine of a big 169 00:07:20,010 --> 00:07:21,780 decimal in other words so with this 170 00:07:21,780 --> 00:07:24,000 approach you can create a calculator 171 00:07:24,000 --> 00:07:26,310 that offers the best of both worlds just 172 00:07:26,310 --> 00:07:28,560 by switching the view model now before I 173 00:07:28,560 --> 00:07:30,569 finish this really is a simple 174 00:07:30,569 --> 00:07:32,460 and it may be overkill to use a view 175 00:07:32,460 --> 00:07:33,659 model class in it 176 00:07:33,659 --> 00:07:35,520 we did get some benefits though even 177 00:07:35,520 --> 00:07:38,009 with an app as simple as this one in the 178 00:07:38,009 --> 00:07:39,719 next video we'll start work on 179 00:07:39,719 --> 00:07:42,150 converting the top 10 downloader app to 180 00:07:42,150 --> 00:07:44,969 use a view model to manage its data see 181 00:07:44,969 --> 00:07:47,719 you in the next video