Last updated on July 13th, 2020 at 11:26 am.
Hi there, and welcome back.
So in the last video we did something about the string, number and the ‘+’ operator.
So in this video, let’s see how you can convert a string to a number in your code.
So if a number is being held in memory as a string as we saw with an example like this, “10”, that means it is being held as a string.
You can convert it to a number before you use it.
So there are different ways of converting a string to a number.
And one of the ways we have is using the ‘parseInt()’ function.
We also have the ‘parseFloat()’ function.
So in the parseInt(), you’ll put the number there, the string there, this is a string, and then this is what is called a ‘radix’.
Now the radix, this is the base of the number.
So, we’re going to look at this in the coming videos.
So I don’t want to talk much about this because we’re going to look at this in detail.
We’re also going to look at this in detail.
And in this video, let me just show you an example using this one.
The ‘unary plus’ is whereby you put the ‘+’ before the string, and then you do the operation.
And the other number, which is also a string, you put a ‘+’ before it, and that will convert the number which is here into, it will convert the string which is here and the string which is here, into a number.
So in this video, let’s do an example with plus.
And as I said, in the coming videos, we’re going to look at these two extensively.
The parseInt() and the parseFloat().
So if I come back to Scratchpad, let me delete all of this utterly and do that.
And only delete these ones.
I can also make this into a string.
So these both are strings.
You can use the double quotes or the single quotes.
Let me say do ‘y+x’, ‘y+x’ if I display, it will give us a concatenation.
It will join this and this.
But now, if I come here and I say, let me enclose this in a bracket and the bracket is used as a good way to just separate things.
So I can say that plus, and that one also enclosed in a bracket and then I add the ‘+’ operator.
So if I add the ‘+’ operator in the front of this number, it’s going to convert this into a number and it’s going to convert this into a number.
And the result we’re going to get is going to be different from this one, which was a concatenation.
So if I display, it will, it will have changed this into a number, changed that into a number and it will give us the result of ‘200+3’.
So also, if I say ‘+100’, let me convert this into string.
And then plus, let me put this side into bracket for easy reading, ‘+200’.
So, it will convert this into a number, and we’ll convert this into a number.
And then it will give us the result of these two.
So if I display, ok, ok, ok.
So first of all, let me remove all of this, because it’s confusing my system.
And then I can display that and it will give us the result of converting this into a ‘100’ and converting that into ‘200’ as ‘300’.
So in this video, we have said that you can convert a string to a number before you use it.
And the method we said you can use are parseInt().
This is a function, that will convert the string you put in there into the number, into a number using the radix you defined.
We’re going to talk about this in the next video.
And then, you can also use the parseFloat().
Now the parseFloat() is also a function that will convert the number into a floating number.
This one converts it to an integer.
This one converts it to a float.
This one to just convert it into whatever you have.
If you have maybe ’11.11’, then you add the ‘+’, and then this side you also add ‘11’, then that is converted into a number.
It will take it the way it is and add them.
This one will return a float and this one return an integer.
So in the next video, let’s look at the parseInt().
If you have any questions, feel very free to let me know.
I’ll see you in the next video.
If you have any questions, let me know.
Click the button below to watch this full course: