3

I'm having a problem when executing parseFloat() - I don't understand why it produces the following outputs:

document.write(parseFloat("6e2") + "<br />"); //output is 600 why?
document.write(parseFloat("6b2") + "<br />"); //output is 6 why?
document.write(parseFloat("6c2") + "<br />"); //output is 6 why?

Could you tell me how the script is working?

erikvimz
  • 5,256
  • 6
  • 44
  • 60
viyancs
  • 2,289
  • 4
  • 39
  • 71

3 Answers3

12

6e2 produces 600 because it's treating your input as scientific notation.

6e2 == 6 x 102 == 600

The other two produce 6 because parseFloat parses the 6, then gets to input it isn't able to convert to a number, so it stops, and returns the result found so far.

Per MDN:

parseFloat is a top-level function and is not associated with any object.

parseFloat parses its argument, a string, and returns a floating point number. If it encounters a character other than a sign (+ or -), numeral (0-9), a decimal point, or an exponent, it returns the value up to that point and ignores that character and all succeeding characters. Leading and trailing spaces are allowed.

If the first character cannot be converted to a number, parseFloat returns NaN.

For arithmetic purposes, the NaN value is not a number in any radix. You can call the isNaN function to determine if the result of parseFloat is NaN. If NaN is passed on to arithmetic operations, the operation results will also be NaN.

Adam Rackis
  • 82,527
  • 56
  • 270
  • 393
  • what the character "E" is special character ? 6e2 = 6 x 10^2; so, E = 10 right?if right? how can?any documentation or reference for that... – viyancs Dec 08 '11 at 02:57
  • Funny that those W3Schools docs aren't quite right. It isn't required that the first character be a number. If there are leading spaces, it will look past those. +1 though. :) – RightSaidFred Dec 08 '11 at 02:57
  • @MohamadSofiyan - yes, essentially. If it reads 6e2 it will be smart enough to realize that you're using scientific notation. – Adam Rackis Dec 08 '11 at 02:58
  • MDN has [a better description](https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/parseFloat) (of course). ;) – RightSaidFred Dec 08 '11 at 02:58
  • @RightSaidFred - thank you. As I've said, I always learn something when you're around. Updating my answer again. – Adam Rackis Dec 08 '11 at 03:01
  • Looks like I was a little to quick to criticize W3Schools. They do have a note lower down that leading and trailing spaces are allowed. Still MDN gives a bit more detail. – RightSaidFred Dec 08 '11 at 03:03
  • @david: He just redeemed himself. – RightSaidFred Dec 08 '11 at 03:05
  • @david - I'm converted to MDN now :) – Adam Rackis Dec 08 '11 at 03:06
  • huzzah! I will remove the downvote, but you're still referring to it as 'the docs' which is lies, it's just a reference site. The real docs would be section 9.3.1 of http://www.ecma-international.org/publications/files/ECMA-ST/Ecma-262.pdf – david Dec 08 '11 at 03:08
  • @David You're right. The description is more accurate now. Incidentally, when I was first learning html/javascript W3Schools was my goto place for info. I guess I can grow up now :) – Adam Rackis Dec 08 '11 at 03:10
  • Sorry for the trouble @Adam. I should've just let it be in the first place. :( – RightSaidFred Dec 08 '11 at 03:13
  • 1
    @Adam I used it too :( till I started noticing that some of the stuff they have is... wrong, it just doesn't work. Not to mention it's full of bad practices. It's a real pity that they're so widely used. – david Dec 08 '11 at 03:14
  • @Right - it was no trouble at all. In fact, you reminded me of an answer I needed to upvote. – Adam Rackis Dec 08 '11 at 03:14
  • @AdamRackis so 6 = 6 x 10 ^ 0; right?i'm still confuse with character "E". b or c is not same with E...sorry i'm newbie for this..:( – viyancs Dec 08 '11 at 03:14
  • @MohamadSofiyan - yes, 6e0 is 6, since anything to the zeroth power is one, so 6 x 10^0 == 6 x 1 == 6. The character E is treated as an exponent. the characters b and c are treated as text, which can't be parsed, which causes pareseFloat to stop and return what it has so far. – Adam Rackis Dec 08 '11 at 03:17
  • i'm sorry can you give me the list of exponen character?i'm still looking for that... – viyancs Dec 08 '11 at 03:46
  • @MohamadSofiyan - as far as I know, only e and E are the exponent characters. – Adam Rackis Dec 08 '11 at 03:49
2

parseFloat() function determines if the first character in the specified string is a number. If it is number then it parses the string until it reaches the end of the number, and it returns the number as a number, not as a string.

so parseFloat("6b2") returns 6.

so parseFloat("6c2") returns 6.

Rushi
  • 4,553
  • 4
  • 33
  • 46
Neel
  • 21
  • 1
0

For the first one, it is because it treats e as an exponent symbol (^)

The other two are only 6 because it ignores the rest once the numbers have ended

blazingkin
  • 552
  • 1
  • 4
  • 17