Three ways to convert a JS string into a number

Source: Internet
Author: User
Tags string to number type casting

There are mainly three kinds of methods

Conversion functions, coercion type conversion, the use of JS variable weak type conversion.

1. Conversion function:

JS provides the parseint () and parsefloat () two conversion functions. The former converts the value to an integer, and the latter converts the value to a floating-point number. These methods are invoked only on the string type, and the two functions are run correctly, and all other types are returned as Nan (not a number).

Some examples are as follows:

parseint ("1234blue");   Returns   1234
parseint ("0xA");   Returns
parseint ("22.5");   Returns
parseint ("Blue");   Returns   NaN
The parseint () method also has a base pattern that converts binary, octal, hexadecimal, or any other string of strings into integers. The base is specified by the second parameter of the parseint () method, as shown in the following example:

parseint ("AF"   );   Returns   175
parseint ("Ten",   2);   Returns   2
parseint ("Ten",   8);   Returns   8
parseint ("ten"   );   Returns   10
If the decimal number contains a leading 0, it is best to use cardinality 10 so that you do not accidentally get the octal value. For example:

parseint ("010");   Returns   8
parseint ("010",   8);   Returns   8
parseint ("010"   );   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 () does not have a base pattern.

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

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. Force type conversions

You can also use coercion type conversion (type casting) to handle the type of the converted value. You can use coercion type conversions to access a specific value, even if it is of another type.
The 3 mandatory type conversions available in ECMAScript are as follows:
Boolean (value)--converts a given value to a Boolean;
Number (value)-Converts a given value to a digit (can be an integer or floating point);
String (value)--converts the given value to a string.
Converting a value with one of these three functions creates a new value that holds the value directly converted from the original value. This can have unintended consequences.
The Boolean () function returns True when the value to be converted is a string with at least one character, a number other than 0 digits, or an object (which is discussed in the next section). If the value is an empty string, number 0, undefined, or null, it returns FALSE.

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

Boolean ("");   False   –   empty   string
Boolean ("Hi");   True   –   non-empty   string
Boolean (m);   True   –   non-zero   number
Boolean (null);   False   -   null
Boolean (0);   False   -   Zero
Boolean (new   Object ());   True   –   object
The force type conversion of number () is similar to the parseint () and parsefloat () methods, except that it converts the entire value, not the partial value. Examples are as follows:
Usage                   result number
(false)                  0 number (
true) 1 number (
undefined)              NaN number
(null 0 Number ("5.5") 5.5 number ("a") Number ("
5.6.7")              NaN
Number (new   Object ())         NaN
(                    100)
The last force type conversion method string () is the simplest and the example 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

For a small example, a look, you will understand.

<script>
var   str= ' 012.345 ';
var   x   =   str-0;
X   =   x*1;
</script>
The above example uses the characteristics of the weak type of JS, only arithmetic operation, realize the type conversion of string to number, but this method is still not recommended










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.