Converting string to number in JavaScript _ javascript skills

Source: Internet
Author: User
This article describes how to convert a string to a number in JavaScript. This article describes three methods to convert a string to a number. For more information, see JavaScript, you can convert a string value to a number using the following three methods:

1. Call Number () to convert the string value type.
2. parseInt ().
3. parseFloat ().

Number ()

It is the most direct method to use the Number () function to forcibly convert the string type. However, this approach has a limitation: If the string does not return a pure numeric string after the blank characters at the beginning and end are truncated, the final return result is NaN. David Flanagan's JavaScript-The Definitive Guide 6th edition, as mentioned in section 3.8.2, when using The Number () function to convert string-to-number, The function only accepts a 10-digit string, however, the test results show that this is not the case. The Number () function can accept "0xff" as a parameter and convert it to a value of 255.


The Code is as follows:


Var a = "42 ";
Var B = "42mm ";
Var c = "0xff ";
Var d = "42.34 ";

Console. log (Number (a); // 42
Console. log (Number (B); // NaN
Console. log (Number (c); // 255
Console. log (Number (d); // 42.34

ParseInt ()

The parseInt () function can convert a string into an integer. Compared with the Number () function, the parseInt () function can not only parse a pure numeric string, you can also parse some numeric strings starting with a number (non-numeric strings are removed during conversion ). It is worth noting that when the parseInt () function parses a floating point string, the method used for the Integer Operation is "truncate ).

In addition to the string as the first parameter, the parseInt () function can take any integer between 2 and 36 as the second parameter to specify the hexadecimal number during the conversion process.


The Code is as follows:


Var B = "42mm ";
Var c = "0xff ";
Var x = "-12.34 ";
Var y = "15.88 ";
Var z = "101010 ";

Console. log (parseInt (B); // 42
Console. log (parseInt (x); //-12
Console. log (parseInt (y); // 15
Console. log (parseInt (c); // 255
Console. log (parseInt (z, 2); // 42
Console. log (parseInt (". 1"); // NaN

ParseFloat ()

Like parseInt (), parseFloat () can also parse some numeric strings starting with a number (non-numeric strings are removed during conversion ). Unlike parseInt (), parseFloat () can be used to convert a string to a floating point number. At the same time, parseFloat () only accepts one parameter and can only process a 10-digit string.


The Code is as follows:


Var c = "0xff ";
Var d = "42.34 ";

Console. log (parseFloat (c); // 0, because "0xff" start with 0
Console. log (parseFloat (d); // 42.34
Console. log (parseFloat (". 1"); // 0.1

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.