Definition and usage of parseFloat (parameter 1, parameter 2) in js and precautions

Source: Internet
Author: User

Today, we used parseFloat (the first parameter, 10) in jquery. We didn't know what the second parameter meant. So I checked it online and wrote it down if I forgot it later.

Definition and usage: The parseFloat () function parses a string and returns a floating point number.

Syntax: parseFloat (string). The string parameter is required, indicating the string to be parsed.
Return Value: return the parsed number.

Notes:
1. spaces at the beginning and end are allowed.
2. If the first character of a string cannot be converted to a number, parseFloat () returns NaN
3. If you only want to parse the integer part of a number, use the parseInt () method.
4. Only the first digit in the string will be returned.

Attached example:
Copy codeThe Code is as follows:
<Script type = "text/javascript">
Document. write (parseFloat ("10 "))
Document. write (parseFloat ("10.00 "))
Document. write (parseFloat ("10.33 "))
Document. write (parseFloat ("34 45 66 "))
Document. write (parseFloat ("60 "))
Document. write (parseFloat ("40 years "))
Document. write (parseFloat ("He was 40 "))
</Script>

Output in sequence:
10
10
10.33
34
60
40
NaN
Next, let's talk about the second question: what does "10" in parseFloat (the first parameter, 10) mean? The second parameter of parseFloat indicates the hexadecimal conversion, and "10" indicates the conversion to "10 ".

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.