Convert JavaScript strings to numbers and numbers to strings

Source: Internet
Author: User
Tags add numbers type casting

Convert JavaScript strings to numbers

Converts a string to a number.ParseintFunction.

Parseint (string): The function is parsed from the string and returns an integer.

Example: parseint ('20140901'): returns 123 (INT );

Parseint ('1234xxx'): 1234 (INT) is returned );

If no number is parsed, a nan value is returned and can be detected using the isnan () function;

Example:

VaR I = parseint ('abc ');

If (isnan (I ))

{

Alert ('nan value ');

}

The same parsefloat function converts a string to a floating point number.

Example: parsefloat ('31. 24abc'): Return 31.24;

Convert JavaScript numbers to strings

Converts a string to a number.StringClass tostring Method

Example:

VaRI = 10;

VaRS = I. tostring ();

Alert (typeof S );// Output string

Differences between JS numbers and strings

The addition of numbers in JS and the connection between strings are both + symbols. Therefore, whether it is a plus or a string connection depends on the type of the variable.

Example:

VaR A = 'abc' + 'xyz ';// The value of A is abcxyz, and the string is connected to the string

VaR A = 10 + 5;// The value of A is 15, and the number is plus.

VaR A = 'abc' + 10;// The value of A is abc10, a string and a number. The value of 10 is automatically converted to a string.

VaR A = 'abc' + 10 + 20 + 'cd ';// The value of A is abc1020cd.

VaR A = 10 + 20 + 'abc' + 'cd ';// The value of A is 30 abccd. You can add numbers first and then connect them.

 

Supplement:

JS String Conversion numeric
There are three main methods

Conversion functions, forced type conversion, and weak type conversion using JS variables.

1. conversion functions:

JS provides two conversion functions: parseint () and parsefloat. The former converts the value to an integer, and the latter converts the value to a floating point number. The two functions can run correctly only when these methods are called for the string type. Nan (not a number) is returned for other types ).

Some examples are as follows:

Parseint ("1234 blue"); // returns 1234
Parseint ("0xa"); // returns 10
Parseint ("22.5"); // returns 22
Parseint ("blue"); // returns Nan

The parseint () method also has the base mode, which can convert binary, octal, hexadecimal, or any other hexadecimal string to an integer. The base is specified by the second parameter of the parseint () method, for example:

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 the leading 0, it is best to use base 10 to avoid unexpected octal values. For example:
Parseint ("010"); // returns 8
Parseint ("010", 8); // returns 8
Parseint ("010", 10); // returns 10

The parsefloat () method is similar to the parseint () method.
Another difference between 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 parsefloat:
Parsefloat ("1234 blue"); // 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. Forced type conversion

You can also use type casting to process the type of the converted value. You can use forced type conversion to access a specific value, even if it is another type.
The three types of mandatory conversions available in ecmascript are as follows:
Boolean (value) -- converts a given value to the boolean type;
Number (value) -- converts a given value to a number (which can be an integer or floating point number );
String (value) -- converts a given value to a string.
Use one of the three functions to convert a value and store the value directly converted from the original value. This will cause unexpected consequences.
If the value to be converted is a string of at least one character, a non-zero number, or an object, the Boolean () function returns true. If the value is a Null String, number 0, undefined, or null, it returns false.

You can use the following code snippet to test the forced type conversion of the boolean type.

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 forced type conversion of number () is similar to that of parseint () and parsefloat (), except that it converts the entire value instead of the partial value. Example:

Result through Method
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
No. (100) 100

The last forced type conversion method string () is the simplest. The example is as follows:

VaR S1 = string (null); // "null"
VaR onull = NULL;
VaR S2 = onull. tostring (); // won't work, causes an error

3. Weak type conversion using JS Variables

Let's take a small example. At first glance, we will understand.
<SCRIPT>
VaR STR = '012. 345 ';
VaR x = The str-0;
X = x * 1;
</SCRIPT>

In the previous example, the weak type of JS is used, and only arithmetic operations are performed to convert the string type to the number type. However, this method is not recommended.

 

Related Article

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.