The implementation of converting JS strings into numbers and numbers into strings

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

  This article is mainly about the conversion of JS string into digital and digital conversion into a string of the implementation of a detailed introduction, the need for friends can come to the reference, I hope to help you.

The JS string converts to a number   converts a string to a number, and the parseint function is used. parseint (String): The function resolves from the beginning of a string and returns an integer.     Example: parseint (' 123 '): return 123 (int); parseint (' 1234xxx '): Return to 1234 (int);   If a number is not resolved, the value of a Nan is returned, which can be used with isNaN ( ) function to detect;   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 '): Back to 31.24     JS number converted to string     converts a string to a number using the string-class ToString Method &nbsp ; Example: var i = 10; var s = i.tostring (); Alert (typeof s); The difference between the output string     JS number and the string   JS The addition of the number and the string connection are all + symbols, so whether the addition or string connection depends on the type of variable.   Example: var a = ' abc ' + ' xyz '; The value of a is: abcxyz, string and string are connected to var a = 10 + 5; The value of a is: 15, the number is plus var a = ' abc ' + 10; The value of a is: ABC10, strings and numbers, automatically converts 10 to a string of var a = ' abc ' + + ' CD '; The value of a is: abc1020cd var a = + + ' abc ' + ' CD '; The value of a is: 30ABCCD, can be digitally added first, and then connect   add:   JS string conversion number. Methods there are three kinds of   conversion functions, forced type conversion, and weak type conversion using JS variable.   1. Conversion functions:   JS provides parseint () and parsefloat () two conversion functions. The former converts the value to an integer, and the latter converts the value to a floating-pointNumber. 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"); The returns NaN   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); The returns   Parsefloat () method is similar to the parseint () method. 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.   Below is an example 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. Coercion type conversion   can also use coercion type conversion (type casting) to handle the type of the converted value. Use coercion type conversion 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 float); 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 ()); The True–object   number () coercion type conversion 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 ("56") ") Number (" 5.6.7 ") Nan Number (new Object ()) Nan number (s)      The last Coercion type conversion method string () is the simplest, as the following example:   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, it will be clearIt's white. <script> var str= ' 012.345 '; var x = str-0; x = x*1; </script>   The example uses the character 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.