Do you really know parsefloat?

Source: Internet
Author: User

I found something about the parsefloat () function of JavaScript. I wrote it down and shared it with you. I also took a note.

The following is a description of the parsefloat function on W3C. Parsefloat is a global function and does not belong to any object. Parsefloat parses its string parameter into a floating point and returns it. If you encounter characters other than the plus or minus sign (+ or-), number (0-9), decimal point, or index (E or E) in the scientific notation, it ignores the character and all subsequent characters and returns the resolved floating point number. At the same time, the blank characters at the first part of the parameter string are ignored. If the first character of the parameter string cannot be parsed as a number, parsefloat returns Nan.

This only illustrates the usage of the parsefloat function and does not explain its mechanism. Let's take a look at what es5 standards say.

That is to say, first call the tostring method for the passed parameter, then remove the space from the resulting string, and then convert it to a number. If it cannot be converted to a number, Nan is returned.


However, I found through my own experiments (in IE, FF, Chrome) that the browser has made some extensions for this. Check the code.

VaR return_numstr = {tostring: function () {console. log ("tostring invoke"); Return "6.5536"; // returns a string that can be converted into numbers}, valueof: function () {console. log ("valueof invoke"); Return "6.5536" ;}} parsefloat (return_numstr); // result: tostring invoke !! Return 6.55.36var return_not_numstr_butprimitive = {tostring: function () {console. log ("tostring invoke"); Return true; // The returned value belongs to the primitive type, but cannot be converted to a number}, valueof: function () {console. log ("valueof invoke"); Return "6.5536" ;}} parsefloat (Return_not_numstr_butprimitive); // Result: tostring invoke !! Return Nan! VaR return_not_primitive = {tostring: function () {console. log ("tostring invoke"); Return {}; // The returned value is not of the primitive type}, valueof: function () {console. log ("valueof invoke"); Return "6.5536" ;}} parsefloat (return_not_primitive); // result: tostring invoke, valueof invoke too !!! Return 6.5536


Based on the above code, we can summarize the following:

1. when parsefloat is called, The tostring method of the input parameter is called first. If the tostring method returns the primitive type (number, String, Boolean, null, undefined ), convert the value of the tostring method to a number to return the result. For example, convert "6.5536" to the number 6.5536. See example 1. If the return value is true or false, it is not converted to 1 or 0, but Nan. For details, see Example 2.

2. If the tostring method returns the object type, the valueof method of the parameter will be called (this es5 specification is not described ). For more information, see Example 3. Here, both the tostring and valueof methods are called, and the tostring method is called first. The returned value {} (not of the primitive type) is used to continue to call the valueof method.

3. Convert the value of the valueof method to a number and return it. (Note that true and false do not return 1 and 0, but Nan)

Do you really know parsefloat?

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.