HEY WHAT'S UP GUYS I'M Jacob Bledsoe with Dev slopes that I'm in the last lesson we got our location
updating for the device to keep track of distance which is really cool but we ran into one problem at
the end.
It was displaying everything in meters.
And while that's not what we want we want to display everything in feet and we also want to round to
the nearest hundredth place for decimals.
So that's what we're going to do.
I'm going to teach you how to do some cool type extensions for the double.
And then we'll also be doing some other things like and date extensions later on and we're going to
do a lot of cool extensions for just re-usability for formatting.
And it's going to be really cool.
So as you remember last time well before we get started let's go ahead and open up our terminal and
I'm going to create a new branch get check out that should be Lesson 8.
So if you need anything from this lesson you can go straight to this branch from the source code.
All right.
Now you can get going so if you remember let me just run the app again.
And if you hit start run you're going to see our distance update and and if it's not moving remember
de-bug location and just put it on city run.
And as you can see crazy amount of decimal places and it's also displaying meters we don't want that.
So let's end this simulator and let's right click our extensions folder and say new file and we just
wanted a swift file.
And I'm just going to call mine extensions and I'm going to be putting everything in this folder.
And create.
Right now as easy as extending a class you can just go extension and we're going to extend double and
then opening and closing brackets.
So one thing we need to do we need to convert meters to Miles Right.
So let's go ahead and call our first function
meters to Miles.
And let's see we also
we're going to want to round this to
what you call it to the 100th or the 100th place.
So let's go ahead and maybe make this a little more use of all we can actually pass in places as an
integer.
And what places will be is how many decimal places we want to display.
OK.
And then we're going to return a double value.
OK.
So to round to the nearest a certain decimal place you need a divisor.
So let's go ahead and create that divisor.
And the way this works is you need to use the the power function that's a built in math tool and how
this works is let's see if we are going to take I'm just going to type this out and then explain it
to you.
OK so how this works is let's pretend we pass and.
Right.
We want it to the 100th place so point 0 0 we want that's what we want.
So we're going to take 10 to the power of two which is a hundred and then we take that 100 square root
it which is 10 again.
And the reason I just picked that is because it's a super easy example for what this is doing.
And obviously whatever you change this places to you you get slightly different numbers.
And then if you multiply the number that we want by the divisor and then divide by the divisor it will
take you to the right decimal place that you need.
So with this override we're going to return return.
We need a couple of parentheses here.
And we're going to say self and that is the number that we're going to be changing from meters to Miles
and I'll show you how to use that in a moment and we need to divide this by 1600 in nine point three
four.
And that is how many meters are in a mile.
So there is one thousand six hundred nine meters.
So in in one mile.
So we're dividing whatever meters we're receiving by how many are in a mile and that's going to return
a mile and then we need to multiply this by our divisor.
And and our parentheses.
And then we also want to round this rounded and with that being rounded we just divide by our divisor
and that's going to return a nicely rounded function or number for us.
So let's go back to our current run location.
And if you remember our run distance is a double.
So now that self that we were dividing by that's run distance.
And since we overwrote it all the way to do is or added the extension is do with the dot and then we
do look for meters to Miles.
Pretty sweet.
And if we pass in a two that's going to return to the nearest two decimal places.
So let's go ahead and run this.
And let's start our run and look at that.
It's not spitting out crazy amount of numbers and it is giving us what we think is is Miles obviously
we'll have to test it later.
But I did take us out for a run and all my math did work.
It matched my Apple Watch.
That was pretty cool.
So we just added an extension to type double which is really cool.
And you can see just the power behind it.
So let's change this place to let's say we want four places just to show that our function works.
But we made this and it's super reuseable now and just adding an extension to type double now allows
us to convert meters to Miles with any double number.
Is really cool.
And there we go.
So now it's showing four decimal places which is really cool.
All right guys.
So I'm going to leave that one there.
Let's go ahead and put this back to to before we end this let's pull up our terminal.
Do a get ad period get commit dash em and we'll say we extended time double to convert meters to Miles.
Cool.
All right great guys.
Next the next place we're going to be going is we're going to be setting up a timer.
So when the run starts we're going to be starting a timer and then.
Yeah.
And then after that we have to just figure out the pace and we are then ready to go for one run but
we start to save all of our data to realms that we're we're almost get into the meat of this course
and I will see you guys in the next one.