1
00:00:05,710 --> 00:00:12,780
HEY WHAT'S UP GUYS I'M Jacob Bledsoe with Dev slopes that I'm in the last lesson we got our location

2
00:00:12,840 --> 00:00:18,180
updating for the device to keep track of distance which is really cool but we ran into one problem at

3
00:00:18,180 --> 00:00:18,680
the end.

4
00:00:18,680 --> 00:00:20,610
It was displaying everything in meters.

5
00:00:20,630 --> 00:00:27,000
And while that's not what we want we want to display everything in feet and we also want to round to

6
00:00:27,000 --> 00:00:30,970
the nearest hundredth place for decimals.

7
00:00:31,020 --> 00:00:32,410
So that's what we're going to do.

8
00:00:32,410 --> 00:00:38,670
I'm going to teach you how to do some cool type extensions for the double.

9
00:00:38,670 --> 00:00:44,430
And then we'll also be doing some other things like and date extensions later on and we're going to

10
00:00:44,430 --> 00:00:48,300
do a lot of cool extensions for just re-usability for formatting.

11
00:00:48,600 --> 00:00:50,950
And it's going to be really cool.

12
00:00:50,970 --> 00:00:56,730
So as you remember last time well before we get started let's go ahead and open up our terminal and

13
00:00:56,730 --> 00:01:01,640
I'm going to create a new branch get check out that should be Lesson 8.

14
00:01:01,980 --> 00:01:08,640
So if you need anything from this lesson you can go straight to this branch from the source code.

15
00:01:08,640 --> 00:01:09,030
All right.

16
00:01:09,030 --> 00:01:13,300
Now you can get going so if you remember let me just run the app again.

17
00:01:16,870 --> 00:01:22,540
And if you hit start run you're going to see our distance update and and if it's not moving remember

18
00:01:22,540 --> 00:01:25,570
de-bug location and just put it on city run.

19
00:01:25,810 --> 00:01:32,420
And as you can see crazy amount of decimal places and it's also displaying meters we don't want that.

20
00:01:32,440 --> 00:01:40,840
So let's end this simulator and let's right click our extensions folder and say new file and we just

21
00:01:40,840 --> 00:01:43,780
wanted a swift file.

22
00:01:44,030 --> 00:01:54,410
And I'm just going to call mine extensions and I'm going to be putting everything in this folder.

23
00:01:54,830 --> 00:01:57,160
And create.

24
00:01:57,320 --> 00:02:09,020
Right now as easy as extending a class you can just go extension and we're going to extend double and

25
00:02:09,020 --> 00:02:11,550
then opening and closing brackets.

26
00:02:11,570 --> 00:02:19,630
So one thing we need to do we need to convert meters to Miles Right.

27
00:02:19,970 --> 00:02:23,390
So let's go ahead and call our first function

28
00:02:25,640 --> 00:02:29,450
meters to Miles.

29
00:02:29,810 --> 00:02:33,200
And let's see we also

30
00:02:37,230 --> 00:02:41,390
we're going to want to round this to

31
00:02:44,300 --> 00:02:49,230
what you call it to the 100th or the 100th place.

32
00:02:49,250 --> 00:02:56,990
So let's go ahead and maybe make this a little more use of all we can actually pass in places as an

33
00:02:56,990 --> 00:02:58,080
integer.

34
00:02:59,620 --> 00:03:04,740
And what places will be is how many decimal places we want to display.

35
00:03:04,810 --> 00:03:05,430
OK.

36
00:03:05,590 --> 00:03:08,130
And then we're going to return a double value.

37
00:03:08,170 --> 00:03:08,710
OK.

38
00:03:08,770 --> 00:03:14,260
So to round to the nearest a certain decimal place you need a divisor.

39
00:03:14,260 --> 00:03:19,150
So let's go ahead and create that divisor.

40
00:03:19,150 --> 00:03:31,540
And the way this works is you need to use the the power function that's a built in math tool and how

41
00:03:31,540 --> 00:03:39,850
this works is let's see if we are going to take I'm just going to type this out and then explain it

42
00:03:39,850 --> 00:03:40,870
to you.

43
00:03:41,380 --> 00:03:44,370
OK so how this works is let's pretend we pass and.

44
00:03:44,390 --> 00:03:44,730
Right.

45
00:03:44,740 --> 00:03:50,090
We want it to the 100th place so point 0 0 we want that's what we want.

46
00:03:50,350 --> 00:04:00,090
So we're going to take 10 to the power of two which is a hundred and then we take that 100 square root

47
00:04:00,090 --> 00:04:01,650
it which is 10 again.

48
00:04:01,700 --> 00:04:08,350
And the reason I just picked that is because it's a super easy example for what this is doing.

49
00:04:08,360 --> 00:04:14,000
And obviously whatever you change this places to you you get slightly different numbers.

50
00:04:14,180 --> 00:04:22,400
And then if you multiply the number that we want by the divisor and then divide by the divisor it will

51
00:04:22,400 --> 00:04:26,840
take you to the right decimal place that you need.

52
00:04:26,840 --> 00:04:35,700
So with this override we're going to return return.

53
00:04:35,930 --> 00:04:37,940
We need a couple of parentheses here.

54
00:04:38,000 --> 00:04:45,700
And we're going to say self and that is the number that we're going to be changing from meters to Miles

55
00:04:45,760 --> 00:04:52,960
and I'll show you how to use that in a moment and we need to divide this by 1600 in nine point three

56
00:04:52,960 --> 00:04:54,330
four.

57
00:04:54,790 --> 00:05:02,730
And that is how many meters are in a mile.

58
00:05:02,740 --> 00:05:05,560
So there is one thousand six hundred nine meters.

59
00:05:05,650 --> 00:05:08,700
So in in one mile.

60
00:05:08,830 --> 00:05:15,580
So we're dividing whatever meters we're receiving by how many are in a mile and that's going to return

61
00:05:15,580 --> 00:05:19,670
a mile and then we need to multiply this by our divisor.

62
00:05:20,830 --> 00:05:22,920
And and our parentheses.

63
00:05:23,070 --> 00:05:31,590
And then we also want to round this rounded and with that being rounded we just divide by our divisor

64
00:05:32,100 --> 00:05:39,480
and that's going to return a nicely rounded function or number for us.

65
00:05:39,510 --> 00:05:41,990
So let's go back to our current run location.

66
00:05:42,450 --> 00:05:45,790
And if you remember our run distance is a double.

67
00:05:45,840 --> 00:05:49,760
So now that self that we were dividing by that's run distance.

68
00:05:49,920 --> 00:05:55,740
And since we overwrote it all the way to do is or added the extension is do with the dot and then we

69
00:05:55,740 --> 00:05:57,960
do look for meters to Miles.

70
00:05:57,960 --> 00:05:59,010
Pretty sweet.

71
00:05:59,220 --> 00:06:05,580
And if we pass in a two that's going to return to the nearest two decimal places.

72
00:06:05,580 --> 00:06:10,620
So let's go ahead and run this.

73
00:06:10,870 --> 00:06:15,710
And let's start our run and look at that.

74
00:06:15,750 --> 00:06:23,520
It's not spitting out crazy amount of numbers and it is giving us what we think is is Miles obviously

75
00:06:23,520 --> 00:06:25,710
we'll have to test it later.

76
00:06:25,920 --> 00:06:29,540
But I did take us out for a run and all my math did work.

77
00:06:29,580 --> 00:06:31,230
It matched my Apple Watch.

78
00:06:31,300 --> 00:06:32,660
That was pretty cool.

79
00:06:32,670 --> 00:06:36,940
So we just added an extension to type double which is really cool.

80
00:06:37,080 --> 00:06:40,730
And you can see just the power behind it.

81
00:06:41,130 --> 00:06:48,000
So let's change this place to let's say we want four places just to show that our function works.

82
00:06:48,000 --> 00:06:55,080
But we made this and it's super reuseable now and just adding an extension to type double now allows

83
00:06:55,080 --> 00:06:59,370
us to convert meters to Miles with any double number.

84
00:06:59,400 --> 00:07:00,450
Is really cool.

85
00:07:01,590 --> 00:07:02,400
And there we go.

86
00:07:02,400 --> 00:07:05,780
So now it's showing four decimal places which is really cool.

87
00:07:08,580 --> 00:07:09,520
All right guys.

88
00:07:09,540 --> 00:07:11,180
So I'm going to leave that one there.

89
00:07:11,190 --> 00:07:16,320
Let's go ahead and put this back to to before we end this let's pull up our terminal.

90
00:07:16,350 --> 00:07:33,410
Do a get ad period get commit dash em and we'll say we extended time double to convert meters to Miles.

91
00:07:33,410 --> 00:07:34,180
Cool.

92
00:07:34,200 --> 00:07:35,710
All right great guys.

93
00:07:35,820 --> 00:07:40,750
Next the next place we're going to be going is we're going to be setting up a timer.

94
00:07:40,770 --> 00:07:47,250
So when the run starts we're going to be starting a timer and then.

95
00:07:47,370 --> 00:07:47,930
Yeah.

96
00:07:48,030 --> 00:07:56,670
And then after that we have to just figure out the pace and we are then ready to go for one run but

97
00:07:56,700 --> 00:08:02,070
we start to save all of our data to realms that we're we're almost get into the meat of this course

98
00:08:02,430 --> 00:08:04,500
and I will see you guys in the next one.
