JS string convert to numbers
Convert the string to numbers and use the PARSEINT function.
PARSEINT (String): The function starts from the start of String and returns an integer.
Example:
Parseint ('123'): Return 123 (int);
Parseint ('1234xxx'): Return 1234 (int);
If the analysis is not available, the value of a nan will be returned. You can use the Isnan () function to detect;
Example:
var I = Parseint ('abc');
if (isnan (i))
{{
alert ('nan value');
}
The same PARSEFLOAT function is to convert the string into a floating point number.
Example: Parsefloat ('31 .24ABC '): Return 31.24;
JS number converts into string
Convert the string to numbers and use the Tostring method of the String class
Example:
var I = 10;
var s = I.tostring ();
alert (typeof s); // The output string
The difference between JS numbers and string
The connection between the number of JS numbers and the string is+ symbols, so the connection of the string is decided to be the type of the variable.
Example:
var a = 'abc' + 'xyz'; // A's value is: abcxyz, string and string are connected
VAR A = 10 + 5; // A value is: 15, the number is plus
var a = 'abc' + 10; // a value is: ABC10, string and numbers, automatically convert 10 into string
var a = 'abc' + 10 + 20 + 'cd'; // a value is: abc1020cd
VAR A = 10 + 20 + 'ABC' + 'CD'; // A value is: 30ABCCD, the number can be added before
Replenish:
JS string conversion number. There are three main methods
Convert functions, compulsory type conversion, and use JS variable weak type conversion.
1. Conversion function:
JS provides two conversion functions: Parseint () and PARSEFLOAT (). The former converts the value into an integer, and the latter converts the value into a floating point number. Only by calling these methods for the String type can these two functions run correctly; the other types are returned to 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 matte pattern, which can convert binary, octagonal, hexadecimal or other inlet string into an integer. The base is specified by the second parameter of the PARSEINT () method. The example is as follows:
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 front guide 0, it is best to use the base 10, so that it will not accidentally get the value of the octal. For example:
Parseint ("010"); // ReturnS 8
Parseint ("010", 8); // Return 8
Parseint ("010", 10); // ReturnS 10
The PARSEFLOAT () method is similar to the processing method of the Parseint () method.
Another difference of using the PARSEFLOAT () method is that the string must represent the floating point number in the form of decimal, and the PARSEFLOAT () has no 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"); // Return 908
PARSEFLOAT ("Blue"); // Return Nan
2. Compulsory type conversion
You can also use Type Casting to process the type of conversion value. Using compulsory type conversion can access specific values, even if it is another type.
The three compulsory types available in ECMAScript are converted as follows:
BOOLEAN (Value) -Stize the given value to BOOLEAN type;
Number (Value) -Convert the given value into numbers (can be an integer or floating point);
String (Value) -Convert the given value into a string.
With one of these three functions, the conversion value will create a new value and store the value directly converted from the original value. This will cause unexpected consequences.
When the value to be converted is at least one character string, non -0 numbers or objects, the Boolean () function will return True. If this value is an empty string, number 0, undefined, or null, it will return false.
You can use the following code segment to test the forced type conversion of Boolean.
Boolean (""); // False Empty String
Boolean ("hi"); // true non-dpty string
Boolean (100); // True Non-Zero Number
Boolean (null); // false -null
Boolean (0); // False -Zero
Boolean (new object ()); // true object
The compulsory type conversion of number () is similar to the processing method of PARSEINT () and PARSEFLOAT (), but it is converted to the entire value, not part of the value. The example is as follows:
Usage result
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 compulsory type conversion method string () is the simplest. The example is as follows:
var s1 = string (null); // "null"
varming = null;
var s2 = Onull.tostring (); // Won'T Work, Causes An error
3. Use JS variable weak type conversion
For a small example, you will understand at a glance.
<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, and only the arithmetic operation is performed to achieve the type conversion of the string to the number. However, this method is not recommended