Javascript parsefloat Internal parsing rules

Source: Internet
Author: User

This is a discussion caused by a problem in the small XI fat, and after the end everyone deepened the understanding of many parsefloat.

As follows

16 binary number 0x10 use parsefloat to turn into numbers, and the result is 0. The subconscious expects the result to be 16.

Some people say that the brain is disabled, 16 of the number of binary why to use parsefloat, because 16 binary is not divided into int and float (that is the 10 binary). It is true that the actual project cannot do so, and this is the only way to discuss it.

Look at the explanation of parsefloat in the norm.

The approximate step is to first convert the arguments to a string, remove the whitespace characters, return a non-numeric character to Nan, and finally return a valid floating-point number. Note that the other characters are not parsed and ignored directly. So for ' 0x10 ', starting with 0 parsing, the x is stopped, and the last 0 is returned.

As follows

This proves the case, and the non-numeric characters stop parsing.

But with one exception,

' 2e ' as above, E ignores the return of 2, but ' 2e2 ' returns 200

What is the reason for this? Because the E2 in ' 2e2 ' is a power operation, that is, 10*10, after parsing to E, it is found that there are no numbers left behind.

More straight explanations on MDN.

That is, parsefloat only handles "+ +", "0-9", decimal point, and Symbol e, and stops parsing when other characters are encountered.

Related:

Https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/parseFloat

Javascript parsefloat Internal parsing rules

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.