Three ways to convert a JS string into a number

Source: Internet
Author: User
Tags type casting

When JS reads a text box or other form data, the value obtained is a string type, such as two text boxes A and B, if the value of a value of 11,b is 9, then A.value is less than B.value, Because they are all in the form of strings. On the internet to find a bit of JS string to the number of the article, the comparison of all

There are three main methods of

Conversion function, coercion type conversion, using JS variable weak type conversion.

1. Conversion function:

JS provides two conversion functions for parseint () and parsefloat (). The former converts the value into an integer, which converts the value into a floating-point number. Only these methods are called on the string type, and the two functions function correctly, and all other types return Nan (not a number).

Some examples are as follows:

Copy CodeThe code is as follows:
parseint ("1234blue"); Returns 1234
parseint ("0xA"); Returns 10
parseint ("22.5"); Returns 22
parseint ("Blue"); Returns NaN

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, as in the following example:

Copy CodeThe code is as follows:
parseint ("AF", 16); Returns 175
parseint ("10", 2); Returns 2
parseint ("10", 8); Returns 8
parseint ("10", 10); Returns 10

If the decimal number contains a leading 0, it is best to use cardinality 10 so that the octal value is not unexpectedly obtained. For example:

Copy CodeThe code is as follows:
parseint ("010"); Returns 8
parseint ("010", 8); Returns 8
parseint ("010", 10); Returns 10

The Parsefloat () method is similar to how the parseint () method is handled.
Another difference between using the Parsefloat () method is that the string must represent a floating-point number in decimal form, and parsefloat () has no base mode.

The following is an example of using the Parsefloat () method:

Copy CodeThe code is as follows:
Parsefloat ("1234blue"); Returns 1234.0
Parsefloat ("0xA"); Returns NaN
Parsefloat ("22.5"); Returns 22.5
Parsefloat ("22.34.5"); Returns 22.34
Parsefloat ("0908"); Returns 908
Parsefloat ("Blue"); Returns NaN

2. Forcing type conversions

You can also use the coercion type conversion (type casting) to handle the type of the converted value. A specific value can be accessed using a coercion type conversion, even if it is of a different type.
The 3 types of coercion conversions available in ECMAScript are as follows:
Boolean (value)-converts the given value to a Boolean type;
Number (value)-converts the given value to a digit (which can be an integer or a floating point);
String (value)-converts the given value to a string.
Using one of these three functions to convert the value, a new value is created that holds the value converted directly from the original value. This can cause unintended consequences.
The Boolean () function returns True when the value to be converted is a string of at least one character, a non-0 number, or an object (this is discussed in the next section). If the value is an empty string, the number 0, undefined, or null, it returns FALSE.

You can test a Boolean type cast with the following code snippet.

Copy CodeThe code is as follows:
Boolean (""); False–empty string
Boolean ("Hi"); True–non-empty string
Boolean (100); True–non-zero number
Boolean (NULL); False-null
Boolean (0); False-zero
Boolean (New Object ()); True–object

The coercion type conversion of number () is similar to the parseint () and parsefloat () methods, except that it transforms the entire value, not the partial value. Examples are as follows:

Copy CodeThe code is as follows:
Usage results
Number (false) 0
Number (TRUE) 1
Number (undefined) NaN
Number (NULL) 0
Number ("5.5") 5.5
Number ("56") 56
Number ("5.6.7") NaN
Number (new Object ()) NaN
Number (100) 100

The last method of forcing a type conversion is string (), which is the simplest example of the following:

Copy CodeThe code is as follows:
var S1 = String (null); "NULL"
var onull = null;
var s2 = onull.tostring (); Won ' t work, causes an error

3. Using JS variable weak type conversion

A small example, a look, you will understand.

Copy CodeThe code is as follows:
<script>
var str= ' 012.345 ';
var x = str-0;
x = x*1;
</script>

The above example uses the characteristics of the weak type JS, only the arithmetic operation, the implementation of the string to the number of the type conversion, but this method is not recommended


Three ways to convert a JS string into a number

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.