Convert to Digital
ECMAScript provides two ways to convert non-numeric primitive values into numbers, namely parseint () and parsefloat ().
Note: These methods are called only for string types (except number) to be run correctly for other types that return Nan.
For example:
JS Code
var iNum1 = parseint ("1234blue"),//returns 1234 var iNum2 = parseint ("OxA"); returns var iNum3 = parseint ("22.5″"); returns var iNum4 = parseint ("Blue"); Returns NaN
The following other types of conversions
One, type conversion
1. Convert into a string
The interesting thing about the original values of ECMAScript's Boolean values, numbers, and strings is that they are pseudo-objects, which means that they actually have properties and methods.
Such as:
JS Code
var scolor = "Blue";
alert (scolor.length);//outputs "4″
In summary, there are 3 primary primitive values, Boolean values, numbers, and strings that have the ToString () method. All objects defined by ECMAScript have the ToString () method, whether it is a pseudo-object or a real object.
The ToString () method of the Boolean type simply outputs "true" or "false", and the result is determined by the value of the variable:
JS Code
var bfound = false;
Alert (bfound.tostring ());//outputs "false"
The ToString () method of number type is special, it has two modes, the default mode and the base mode, the default mode, and the ToString () method simply outputs the numeric value with the corresponding string (whether it is an integer, a floating-point number, or a scientific notation).
JS Code
var iNum1 = 10; var fNum2 = 10.0; Alert (inum1.tostring ()); Outputs "10″alert (fnum2.tostring ()); Outputs "10″
The base mode of the ToString () method with the number type can be used to output numbers in different bases (radix).
JS Code
var iNum = 10; Alert (inum.tostring (2)); Outputs "1010″alert (inum.tostring (8)); Outputs "12″alert (inum.tostring (16)); Outputs "A"
2. Convert to Digital
ECMAScript provides two ways to convert non-numeric primitive values into numbers, namely parseint () and parsefloat ().
Note: These methods are called only for string types (except number) to be run correctly for other types that return Nan.
For example:
JS Code
var iNum1 = parseint ("1234blue"),//returns 1234 var iNum2 = parseint ("OxA"); returns var iNum3 = parseint ("22.5″"); returns var iNum4 = parseint ("Blue"); Returns NaN
The parseint () method also has a base mode that converts binary, octal, hexadecimal, or any other binary string into a decimal integer. The second parameter specifies which type of binary to parse.
JS Code
var = parseint ("AF", INUM1),//returns 175 var iNum2 = parseint ("10″,2"); Returns 2 var iNum3 = parseint ("10″,8"); Returns 8 var iNum4 = parseint ("10″,10"); Returns 10
Note: If the decimal number contains a leading 0, it is best to use cardinality 10, otherwise you will get the octal value.
JS Code
var iNum1 = parseint ("010″"); Returns 8 var iNum2 = parseint ("010″,8"); Returns 8 var iNum3 = parseint ("010″,10");//returns 10
The Parsefloat () method is similar to the parseint () method, viewing each character starting at position 0 until the first non-valid character is found, and then converting the string before the character to a number. For this method, the first decimal point that appears is a valid character. If you use two decimal points, the second decimal point will be considered invalid. Another difference in using this method is that the string must represent a floating-point number in decimal form.
JS Code
var fNum1 = parsefloat ("1234blue"); Returns 1234.0 var fNum2 = parsefloat ("0xA"); Returns NaN var fNum3 = parsefloat ("22.5″"); Returns 22.5 var fNum4 = parsefloat ("22.34.5″")//returns 22.34 var fNum5 = parsefloat ("0908″");//returns NaN var FNUM6 = parsefloat ("blue");//returns NaN
3. Forcing type conversions
The 3 types of coercion conversions available in ECMAScript are as follows:
(1). Boolean (value)
Converts the given value to a Boolean type.
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.
Such as:
JS Code
var B1 = Boolean (""); False var B2 = boolean ("Hi"),//true var b3 = boolean,//true var b4 = boolean (null),//false var b5 = boolean (0);//f alse var b6 = Boolean (New Object ());//true
(2). Number (value)
Converts the given value to a number (which can be an integer or a floating point).
Remember that the parseint () and parsefloat () methods only convert the string before the first invalid character, so "4.5.6″ will be converted to" 4.5″. Coercion type conversion with number (), "4.5.6″ will return Nan because the entire string value cannot be converted to numbers." If the string can be fully converted, number () will determine whether to call the parseint () method or call the Parsefloat () method.
Such as:
JS Code
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
(3). String (value)
Converts the given value into a string.
The only difference from calling the ToString () method is that coercion of type conversions on null or undefined values can generate strings without throwing an error:
JS Code
var S1 = String (null);//"null" var onull = null; var s2 = onull.tostring ();//causes an error
Ii. type of reference
A reference type is often called a class, that is, when a reference value is encountered, the object is processed. ECMAScript defines "object definitions", which are logically equivalent to classes in other programming languages.
1.Object class
All classes in ECMAScript are inherited by this class, and all properties and methods in the object class appear in other classes (overwritten).
Properties of the object class:
(1). constructor--a reference (pointer) to the function that created the object. For the object class, the pointer points to the original object () function.
(2). prototype--a reference to the object's prototype of the object. For all classes, it returns an instance of Object objects by default. One, type conversion
1. Convert into a string
The interesting thing about the original values of ECMAScript's Boolean values, numbers, and strings is that they are pseudo-objects, which means that they actually have properties and methods.
Such as:
JS Code
var scolor = "Blue";
alert (scolor.length);//outputs "4″
In summary, there are 3 primary primitive values, Boolean values, numbers, and strings that have the ToString () method. All objects defined by ECMAScript have the ToString () method, whether it is a pseudo-object or a real object.
The ToString () method of the Boolean type simply outputs "true" or "false", and the result is determined by the value of the variable:
JS Code
var bfound = false;
Alert (bfound.tostring ());//outputs "false"
The ToString () method of number type is special, it has two modes, the default mode and the base mode, the default mode, and the ToString () method simply outputs the numeric value with the corresponding string (whether it is an integer, a floating-point number, or a scientific notation).
JS Code
var iNum1 = 10; var fNum2 = 10.0; Alert (inum1.tostring ()); Outputs "10″alert (fnum2.tostring ()); Outputs "10″
The base mode of the ToString () method with the number type can be used to output numbers in different bases (radix).
JS Code
var iNum = 10; Alert (inum.tostring (2)); Outputs "1010″alert (inum.tostring (8)); Outputs "12″alert (inum.tostring (16)); Outputs "A"
The parseint () method also has a base mode that converts binary, octal, hexadecimal, or any other binary string into a decimal integer. The second parameter specifies which type of binary to parse.
JS Code
var = parseint ("AF", INUM1),//returns 175 var iNum2 = parseint ("10″,2"); Returns 2 var iNum3 = parseint ("10″,8"); Returns 8 var iNum4 = parseint ("10″,10"); Returns 10
Note: If the decimal number contains a leading 0, it is best to use cardinality 10, otherwise you will get the octal value.
JS Code
var iNum1 = parseint ("010″"); Returns 8 var iNum2 = parseint ("010″,8"); Returns 8 var iNum3 = parseint ("010″,10");//returns 10
The Parsefloat () method is similar to the parseint () method, viewing each character starting at position 0 until the first non-valid character is found, and then converting the string before the character to a number. For this method, the first decimal point that appears is a valid character. If you use two decimal points, the second decimal point will be considered invalid. Another difference in using this method is that the string must represent a floating-point number in decimal form.
JS Code
var fNum1 = parsefloat ("1234blue"); Returns 1234.0 var fNum2 = parsefloat ("0xA"); Returns NaN var fNum3 = parsefloat ("22.5″"); Returns 22.5 var fNum4 = parsefloat ("22.34.5″")//returns 22.34 var fNum5 = parsefloat ("0908″");//returns NaN var fNum6 = Parsefloat ("blue");//returns NaN
3. Forcing type conversions
The 3 types of coercion conversions available in ECMAScript are as follows:
(1). Boolean (value)
Converts the given value to a Boolean type.
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.
Such as:
JS Code
var B1 = Boolean (""); False var B2 = boolean ("Hi"),//true var b3 = boolean,//true var b4 = boolean (null),//false var b5 = boolean (0);//false var b6 = Boolean (New Object ());//true
(2). Number (value)
Converts the given value to a number (which can be an integer or a floating point).
Remember that the parseint () and parsefloat () methods only convert the string before the first invalid character, so "4.5.6″ will be converted to" 4.5″. Coercion type conversion with number (), "4.5.6″ will return Nan because the entire string value cannot be converted to numbers." If the string can be fully converted, number () will determine whether to call the parseint () method or call the Parsefloat () method.
Such as:
JS Code
Number (false),//0 number (true),//1 number (undefined),//nan number (null),//0 number ("5.5″"),//5.5 number ("56″";//56 Nu Mber ("5.6.7″);//nan Number (New Object ());//nan number (100);//100
(3). String (value)
Converts the given value into a string.
The only difference from calling the ToString () method is that coercion of type conversions on null or undefined values can generate strings without throwing an error:
JS Code
var S1 = String (null);//"null" var onull = null; var s2 = onull.tostring ();//causes an error
Ii. type of reference
A reference type is often called a class, that is, when a reference value is encountered, the object is processed. ECMAScript defines "object definitions", which are logically equivalent to classes in other programming languages.
1.Object class
All classes in ECMAScript are inherited by this class, and all properties and methods in the object class appear in other classes (overwritten).
Properties of the object class:
(1). constructor--a reference (pointer) to the function that created the object. For the object class, the pointer points to the original object () function.
(2). prototype--a reference to the object's prototype of the object. For all classes, it returns an instance of Object objects by default.
JQUERY,JS Type Conversions