32 Javascript parsefloat

Learn Javascript from Scratch

Last updated on June 30th, 2018 at 08:57 pm

 

This video is taken from the full course that will teach you HTML, CSS, Programming concepts and Javascript .

 

Watch Full Course >>

 

Transcript :

Hey, and welcome back.

In the last part we were looking at parseInt().

Now, we get to move on to parseFloat().

And the good thing about this, it is not that complicated.

You put in a string and it returns a floating point number.

So, this is not as complex as the other one.

You just put in the number, if you just put in the number within there, it will return to you the floating point number.

So what is a floating point number? A floating point number is a number that has, it is either a positive or a negative number.

It has an integer part.

So maybe 200.

So it may be positive or it may be negative.

So it can be 200 or it can be -200.

And then it also has or it may have a decimal point.

So maybe it can be 200.22, that is a floating point number.

Or it can also be a plus, a positive or a negative number that has an exponent.

So 2 exponent of 2 or negative 2.

So maybe we want to represent 3.99 as an exponent.

This would be 3 exponential of -2 or rather 399 exponential -2 to give us that.

So to represent 3.99, it would be as an exponent, it would be 399e-2.

So this is also a floating point number.

So if a string cannot be parsed, it will return ‘not a number’.

So for instance, this is not possible because this will not give us a decimal number.

So in our case with the parseFloat(), all the numbers must be decimal unlike the other one, where we had certain other radices we could use.

Here we only have decimal numbers.

So it’s a decimal number.

So this cannot be parsed into a number and this will return ‘not a number’.

So if this cannot be parsed, it will return ‘not a number’ and then it will return up to the possible point where it can actually get a real number.

So for example, in this case it’s going to return this up to this point.

It will return it up to this point and it will ignore, it will ignore all these other parts.

So it will return it up to there because at least this makes sense to this function.

So let’s see an example with this, all of this in one long example.

And then, we can end the video.

So if I come back to Scratchpad, if I come back to Scratchpad, I will delete all this.

So this one is now parseFloat().

Now parseFloat(), we don’t need the radix.

So parseFloat(), this one, display.

It will give us that.

If it is 9 and then maybe exponential of 3.

Then we display.

It will give us that.

Then maybe it is 9.33e3.

Display.

It will give us that.

Maybe it is just 9.33, it will return back that.

Let me remove this and then maybe it is 123e-21 and then with the space if we display, it will return this.

So if we reduce this maybe to ‘e-4’ and we display that, at least it gives us something that is like that.

So it returns it up to the point that it can.

So if I say 45e-3 and then exponential again of 32, 34, it will return it up to the point that it can.

So if I do this, it will return it up to that point only.

So it’s going to be 0.045.

So if I display this one, zero, oh come on.

So if I come here, and I display that one it will give us 0.045 and if I add a space in there, it will only give us up to the point that it can.

It cannot parse a space.

And therefore if I display the results of that, it only brings back 45.

And if I make it something that is not understood like this, this can be, maybe this is a hexadecimal number and you try to put it here to convert it into a floating point number using the parseFloat() function.

This will give us ‘not a number’ because this number is not a decimal number.

So if I display that one, it will give us ‘not a number’.

And if I start with a space, let me say I start with a space.

2 space 4, I start with a space.

And then, I try, let me first of all remove all these other results.

Then I try to display that, it will ignore the first space and give us a number.

But, it will not ignore the next space.

The next ones are not ignored.

So bear that in mind.

So it ignores even symbols.

If I come here and I say ‘2+4*4’ and I come here and I try to display this one, it will ignore all these others because they’re not numbers.

It just brings back up to the point that it can.

So that’s it for this video.

In this video we have seen how the parseFloat() works.

And this one is much easier to use because we just put in the number in there.

We don’t have to use any radices.

It just brings back a float.

So it brings back a float and we’ve said that a float is a decimal number.

It has a decimal function, a decimal part, a decimal number in this case.

And then you can have the decimal point.

And then you have the fraction part which in this case is this one.

And it can be a plus or a negative or it can just be a decimal number, plus or negative like it is here and then exponential of a plus or a negative number here, like -2e-2 or negative or +2e-2.

So that’s it for this video.

If you do have any questions, feel very free to let me know and I will answer them for you.

I’ll see you in the next part.

And in the next part, let’s start looking at literals, JavaScript literals.

Literals are just the actual digits or numbers that you use in your function.

So instead of using a variable, va=2, this 2 is a literal.

So see you in the next section.

 

 

 

Click the button below to watch this full course:

Watch Full Course >>

 

 

Comment Here