|
JS String Conversion number 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:
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:
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: 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: 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, not a 0 number, or an object. 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.
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:
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:
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. <script> var str= ' 012.345 '; var x = str-0; x = x*1; </script>
JS String converted to Digital
the parseint function is used to convert a string to a number.
parseint (String): The function parses from the start of a string and returns an integer.
Example: parseint (' 123 '): Returns 123 (int);
parseint (' 1234xxx '): Returns 1234 (int);
If the number is not resolved, a Nan value 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 '): Returns 31.24;
JS numbers converted into strings
To convert a string to a number, use the toString method of the string class
Example:
var i = 10;
var s = i.tostring ();
Alert (typeof s); Outputs a String
The difference between a JS number and a string
The addition of the JS number and the string connection are all the + symbols, so whether the connection is a plus or a string depends on the type of the variable.
Example:
var a = ' abc ' + ' xyz '; The value of a is: ABCXYZ, string is concatenated with 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 ' + Ten + + ' CD '; The value of a is: ABC1020CD
var a = ten + + ' abc ' + ' CD '; The value of a is: 30ABCCD, you can digitally add the first number plus, and then connect
|