The realization method of converting a JS string into a number and a number into a string _javascript tips

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

JS String converted to Numbers

To convert a string to a number, you use the parseint function.
parseint (String): The function resolves from the beginning of a string and returns an integer.


Example:
parseint (' 123 '): Return to 123 (int);
parseint (' 1234xxx '): Return to 1234 (int);

If the number is not resolved, the value of a Nan is returned, which 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 into a floating-point number.

Example: parsefloat (' 31.24abc '): return 31.24;


JS number converted to string


Converts a string to a number and uses the toString method of the string class

Example:
var i = 10;
var s = i.tostring ();
Alert (typeof s); Will output String


The difference between a JS number and a string

JS number of additions and strings of the connection is a + symbol, so whether the addition or string connection depends on the type of variable.

Example:
var a = ' abc ' + ' xyz '; The value of a is: ABCXYZ, the string is connected with the string
var a = 10 + 5; The value of a is: 15, the number is plus
var a = ' abc ' + 10; The value of a is: ABC10, string and number, automatically converts 10 to a string
var a = ' abc ' + + ' CD '; The value of a is: ABC1020CD
var a = + + ' abc ' + ' CD '; The value of a is: 30ABCCD, you can digitally add the first number plus, and then connect

Add:

JS string converts a number. 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 10
parseint ("22.5"); Returns 22
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", 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 you do not accidentally get the octal value. For example:
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 () 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 of at least one character, a number other than 0 digits, or an object. 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 (100); 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 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 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.