When JS reads a text box or other form data, the value obtained is a string type, such as two text boxes A and B, if the value of a value of 11,b is 9, then A.value is less than B.value, Because they are all in the form of strings. On the internet to find a bit of JS string to the number of the article, the comparison of the total method has three conversion functions, forced type conversion, the use of 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:
Copy the code code as follows:
parseint ("1234blue"); Returns 1234PARSEINT ("0xA"); Returns 10PARSEINT ("22.5"); Returns 22PARSEINT ("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:
Copy the code code as follows:
parseint ("AF", 16); Returns 175PARSEINT ("10", 2); Returns 2PARSEINT ("10", 8); Returns 8PARSEINT ("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:
Copy the code code as follows:
parseint ("010"); Returns 8PARSEINT ("010", 8); Returns 8PARSEINT ("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:
Copy the code code as follows:
Parsefloat ("1234blue"); Returns 1234.0parseFloat ("0xA"); Returns Nanparsefloat ("22.5"); Returns 22.5parseFloat ("22.34.5"); Returns 22.34parseFloat ("0908"); Returns 908parseFloat ("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, a non-0 number, or an object (this is discussed in the next section). 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.
Copy the code code as follows:
Boolean (""); False–empty Stringboolean ("HI"); True–non-empty Stringboolean (100); True–non-zero Numberboolean (NULL); False-nullboolean (0); False-zeroboolean (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:
Copy the code code as follows:
Usage results
Number (false) 0Number (true) 1Number (undefined) nannumber (null) 0Number ("5.5") 5.5Number ("") 56Number ("5.6.7") Nann Umber (New Object ()) nannumber (100) 100
The last method of forcing a type conversion is string (), which is the simplest example of the following:
Copy the code code 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
A small example, a look, you will understand.
Copy the code code as follows:
<script>var str= ' 012.345 '; var x = Str-0;x = x*1;</script>
The above example uses the characteristics of the weak type JS, only the arithmetic operation, the implementation of the string to the number of the type conversion, but this method is not recommended