JavaScript numeric conversion number () detailed

Source: Internet
Author: User

---restore content starts---

Number () parseint () parsefloat () All three letters can be converted to numeric values, so let's see what the difference is.

A number ()

Transformation function number () is omnipotent, he can transform any form of data type, the rules are as follows

1. If it is a Boolean, true and False will be converted to 1 and 0, respectively;

2. If it is a number, simply pass in and return;

3. If NULL is converted to 0.;

4. Return Nan if it is undefined;

5. If it is a string-----the rule is more detailed:

1. If the string contains only numbers, (including the preceding exact and minus signs) will be converted to decimal, such as "1" into 1 ", 123" to 12, and "0123" becomes 123 note that the preceding 0 is ignored

2. The string is converted to 0 if it is empty

3. If the string has characters other than those in the above format, it will be converted to Nan;

4. If the object is a method called valueof (), convert it according to the above rules

Code

1 <!DOCTYPE HTML>2 <HTMLLang= "en">3 <Head>4     <MetaCharSet= "UTF-8">5     <title>VIDEO</title>6 </Head>7 <Body>8 <Script>9     varNUM1=Number ("");Ten alert (NUM1);//conversion to 0 One  A      //Code 2 -      varnum2=Number ("Hello"); - alert (num2);//Convert to Nan the      //Code 3 -      varnum3=Number ("0000222"); - alert (num3);//Convert to 222 -      //Code 4 +       varnum4=Number (true); - alert (NUM4);//Conversion 1 +  A  at  -  -  -  - </Script> -      in </Body> - </HTML>

Because number () is complex and unreasonable, parseint () is more commonly used when working with integers. The parseint () function ignores whitespace in front of the string when converting a string, knowing that the first non-whitespace character is found.

If the first character is not a number or minus sign, parseint () returns Nan, and similarly, converting an empty string with parseint () also returns Nan.

If the first character is a numeric character, parseint () continues to parse the second character until all subsequent strings have been parsed or a non-numeric character is encountered.

The parseint () method also has a base mode that converts binary, octal, hexadecimal, or any other binary string into integers.

The base is specified by the second parameter of the parseint () method, so the hexadecimal value is parsed, and, of course, the parseint () method can be called for binary, octal, or even decimal (the default mode).

var num1 = parseint ("AF", 16);            175 var num2 = parseint ("AF");         NaN var num4 = parseint ("Sdasdad"); NaN

  

JavaScript numeric conversion number () detailed

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.