In this lecture, we are going to format numbers to have two decimal spaces, and if there are thousands
or greater the thousands, we should have a comma in the example is like this.
OK, so we can create two functions.
The first function will be for this and I'm going to get rid of these decimals.
I don't want in this function to have decimals in four days.
This and when we click here and food or exercise where we can see the calories, we will use the decimals.
OK, now where we should write these functions because it's connected with APU, a controller, we should
put it there.
The logic is that you can put it even in the calories controller, but it will be super bad if you use
them because here, if you go back to after you are controller right now here we do have this item in
here.
We do have the calories.
Right.
And also we do have this display for an app calories.
And we do some calculations there.
We just grab all of it and just put it there so we can we need to form in these numbers, these two
latitude to burn that will be here.
OK, now that is why we should put it there.
Now, I'm going to create this as a private functions and top.
Why?
Because the closures can use the outer functions can use these functions here to private, even if they're
even if the D want the function is finished.
Right.
And if he was doing us a favor that putting that private functions to hide that data and that is called
data encapsulation.
So I'm talking too much.
But no, for the purpose of this, I'm going to test it here.
I'm not going to add calories all the time.
It's a little bit boring.
I'm going to create them as a public function.
And after that, we're going to what when we finish, when we know that working, we're going to just
move them in the private part.
OK, now I'm just going to say format, format, let's say remaining.
And that obviously will be for for the remaining text.
Right.
So I'm just going I told you, we are going to do two functions for this one.
For this one.
I just want to when we have a thousands like this, I want a comma here.
Two thousand two comma 500.
Right.
Oh, here.
I need to pass in a parameter and let's test it out if it's working comes a lot.
No.
OK, one more thing.
I just delete all of the comments before for this date.
Right.
If you look at a little bit, you'll see that there are no no comments.
Right.
That's not good for me because I don't want to see that code so messed up.
So, Apu, I controller and I can ask I can access not formatting because it's public.
Let's put it here.
One thousand five to zero.
And here is we're getting that output because we are controlling life.
OK, I don't need this actually.
So what is the idea here?
I will use string substract method in the syntaxes stream.
So any stream that we're going to give it to here and as you be SDR, it takes two parameters.
Start in length.
Right?
This one is required and this one is optional.
Require one, specify the position from where to start the extraction.
Now there are two functions similar.
We have string substract and we have additional one string that.
Just wait.
I'm just going to create a variable, I'm going to tell you.
So I'm going to create a variable screen name and that will be Hello World.
Right.
And screen name it will you can do substring or this one that we are going to use now.
They are similar.
Yeah.
But there is a slight difference.
This the first one function returns a specified number of characters from the specified index.
Right.
And that is what we need in our case.
So don't confuse those two are not.
See now the index of the first character is zero.
If the start of the parameter is positive or greater than or equal to the length of the string, then
this function will return an empty string.
If the start parameter is negative, then this function uses it is as an index from the end of the string
and you will know what I'm trying to say.
So then the second parameter is optional.
You specify the number of characters you will extract.
If not pass, it will extract the whole string.
So if we don't specify this one, we will just give you this one.
So that's not what we want.
OK, here, let's start using this.
So string name.
This is just the test.
Don't worry about you.
Don't have to write it in.
I'm sorry.
I'm telling you just this right now.
So cancel the log and we're going to say string name Dot
and we are going to say zero and comma three.
So this zero will start from the zero from because as I told you, you specify the position.
And if the index or the first character is always zero, so this is zero zero one the same as I'm there
right now, this will should give me this zero one to.
Right.
And he should give me help.
And if I access this one, OK, it doesn't matter because we're not bringing up that anything.
So it will give me a hell if I go to number four it will give me a real help and.
So I'm going up arrow arrows and it will give me what I have typed last, so help.
OK, now you know how this abstraction works, but how are we going to use this for our coding?
OK, one more.
We know that we can get the length of string and that string name, for example, that length.
And if we type it down now lot the length string should give me some 11 or 12 characters, probably
11 right now.
If I go here until 10, I would expect to have the string, this whole string back except for the D
areas it's working right now.
We're going to use this length in our core.
So this is pretty much done.
I'm going to kill it and I'm going to say syntax.
You will need only this, right?
So here I just want this.
Whatever we pass here doesn't matter if it's if it's not if it's string or not.
I just want to use number to be equal to math that round now.
And I'm going to pass this number again and I'm saving again in the same variable.
And I can do that.
Right.
I can do that.
No, no problem.
OK, so what I'm doing this because we because we're dealing with decimals here.
When we add we can add decimals.
Fifty five or five for example.
And I don't want to decimals here, I just want to have the same round number.
OK, now in order to use the string, for example, I am going to convert this number to string using
number two.
So I'm going to use this function to string oops and let's count a lot now the number.
So you should give me back spring, right?
So I'm going to go back here isn't passing a no and yeah, it's giving me strength.
How cool is this?
So what I can do now is actually I can say that I've no, that length is greater than three means that
if I have a number with number, it will be equal to one thousand five hundred and twenty.
So the length is greater than three and it's four actually right here.
And what it will go here and say comes along.
OK, right, let's just as all else becomes a lot smaller then.
Okay, well, it's just this is this is working our logic, so I'm just going to copy here.
You should give me a key and this should not give me OK, right.
Smaller than thousand.
Not so.
Our logic is working if the length is greater than three.
So do whatever you want here.
But we want this now.
So I'm just going to call that the entire thing and I'm just going to split this one.
So I'm going to use the substract.
And what I'm going to see here, I'm going to start from the first position and I'm going to say no
that length minus three, and I'm going to concatenate this with a comma, right.
OK, just bear with me and we have one more.
No, not substract no length minus three to start from there and to go until the end.
OK.
No, I am going to cancel lot this number here.
So what will happen.
What you think
it is.
Our function is working, but how we get there.
How did you how did we solve this.
OK, the first part I'm just going to show to you the first part.
What is this part doing?
So that's why we are using the that's why we are using these substract.
So I'm saying that you should start from zero and then the length should go to minus three.
So that means you should go to zero in the lengths you go minus three.
And the length for this one is for one, two, three, and it's finishing here.
That why this part will give me only one.
So I'm trying to get the number one here from this one thousand five hundred and twenty.
Here it is.
I get the one because I'm seeing start from start from zero here.
But the length go minus three.
So the length was four, now it's one.
So I'm getting the first one right in while I'm using this minus four.
You will see now how I get the second part.
So the comma, I just add this comma here.
Right, I just concatenate what I have here.
Now the last part is OK, I just copy something else.
So the next part, ok, I'm saying start from minus three and that minus three will go zero one three.
So start from five and go until the end in three more positions.
One, two, three.
Right.
So that should give me five twenty.
That is the logic.
But you're going to see, OK, how this will work with more than thousands.
Right.
Here it is now.
I have one.
I have five twenty.
And I concatenate this comma here.
But what if I have like what if I have fifteen thousand two hundred.
Right.
It will still work.
Why?
Because I always get this go three times back and after that go until the end.
Right.
Any and I'm saying here, OK, start from zero and go three times.
So it's one, two, three.
That's why the 15 is going there.
That's why this one is left.
So 15 and 20 is left.
So because I'm going from the first position, but I'm seeing here start minus three.
And when you do, minus three is going to left you with fifteen.
So one five will be subtracted from the real screen.
So I hope you understand this point and I hope I just manage to explain it in the other part will be
exactly the same.
But we have a slightly different format, calories, OK, it will still take a number and we will still
use this one.
Right.
But here we are going to do a couple of changes.
We want a decimals in.
First, I'm going to do instead of a round, I'm going to use the absolute.
So that will give me the Maddog APS function returns the absolute value of numbers.
If it's negative, it's going to return to a positive.
So now next, what we need to do is actually to use to fixed and we need to specify how much to for
example, this method is of a number of product and this put two decimal numbers in round.
So actually in round that number.
So we have now a number that is string, but it's not actual number.
Right.
So what we can do I'm just going to comment this out and actually we're not going to need this or else.
Right.
We're not going to use it.
So cancel the number.
Very good.
So I'm going to call this function.
Now, if you are a controller that this and let's say one thousand five hundred and twenty.
And here is it, put two decimal spaces there.
Cool.
Now, what we need to do is actually to use the split now, so I'm going to use the split.
Now, remember, we do have this at zero and we use the split with the for the dash.
Now we're going to use the split four when we have this something like this.
Right.
Or 55 it can be.
So we going to use the split there.
OK, so you already know this function.
We have to use it and I'm going to say split.
Every no.
Why is this one?
Why this name?
Because the split will return missionary, right?
Some no, not split.
And I'm going to say that that should be the split part in the first integer.
I will say split array.
And I'm going to say zero.
That will be the first integer in the decimal part.
It will be split, that one.
All right, because this, ladies and gentlemen, will turn me on every week.
For example, if we give this number right, if we give this number, it will return me.
So it will return me this number as a first integer is the first every element of the error and the
two decimal points, the two decimal points will be second part of the area.
And that's why I get it with number one.
That's the index.
OK, done.
So that sounds a lot deathstar of logic.
First, and it comes of the decimal part.
And I'm just going to put all of the variables on top here, so I'm going to have this one.
Fifty five.
And here it is said, give me 15.
Fifty five in the decimal part is there.
So what I'm going to use now so I'm going to use the same logic from here.
Just kind on comment.
This one else.
We don't need it.
I do have the same number.
I do have the same length.
I'm going to do exactly the same because I'm going to add the comma there.
OK, but now after this is done, because this is going to return me,
it's going to return a value here and I'm going to show it.
It is going to return.
Um, actually, we're not going to return it here.
We're going to return it in the end.
But what we can do is I'm going to say formatter.
Formatted text or formatted number, and that will be equal to first int plus.
Got that small part right, how this will look.
We will say council block the four minute number, because I do have this first and I do have this.
And let's see what will happen.
Right.
Instead of number here, we need to just change the first.
Right.
Actually, we don't need all this.
I just need these first end to be replaced with the number, right, because we don't have a number
here.
We change it.
We use that number to get the split it into two parts.
OK, now let's call this function again hit enter in.
This is what I got.
Right.
So it split the first one, split the second one and that comma from here.
And after that I just add those number because it saved back in the first.
So the commas there actually now and I just attach this, I mean, concatenate with the dot decimal
point and that's it's pretty much now how are we going to write this one inside our public?
So it is working now.
I'm just going to get rid of this before I do that.
Let's create a variable here and let's put it split or a number.
Let's first.
And then I do have the small part, then I do have for my number, and I think we have done.
Now I'm going to copy this.
I'm going to cut actually this function and I'm going to private above it and I'm going to put it here.
I need to change, obviously, things.
So that will be equal to what?
That will be equal to function.
Right.
So that will be equal to a function.
It should be or should be semicolon.
OK, and we not instead of cancelling Locke, you should return this form.
It is no good.
So we're returning this.
OK, let's fix this face here.
I think we are done and let's right here format calories for the front.
You are very good.
Now we have one more.
The first one.
The remaining one.
I'm just going to cut the entire thing and we're going to clean this here.
And we've got pasta here and this should be equal and these should be stored in a variable that, oh,
it should not be sorted.
Don't worry about it.
So what else?
I need to do this.
To do this.
And also, we also need to instead of counseling love, there's no need to return it.
And semicolon here, by the way.
Sorry, guys.
Return the no.
OK, no, cut that space.
I think we are done here and I need to add some comment here.
Format remaining chart that is remaining.
Galeries in our graph chart.
OK, now how where are we going to use this.
So I'm going to just try to keep this lecture as it is.
We need to add them in this item and display front calories and we have done well.
I'm going to do that one in the next lecture because it's going to take too much time.
And I don't want to bother you.
Right.
So I'll see you in the next one.