The three main primitive values, Boolean values, numbers, and strings, all have the tostring () method. Tostring can also contain a parameter, that is, its base mode.
The base mode of the tostring () method of the number type can be used to output numbers using different bases.
<HTML>
<Head> <title> JS test page </title> <Body>
<Span id = "MSG"> </span>
<Script language = "JavaScript">
VaR num1 = 15;
Out (num1.tostring (2 ));
Out (num1.tostring (8 ));
Out (num1.tostring (10 ));
Out (num1.tostring (16 ));
Function out (MSG ){
VaR OBJ = Document. getelementbyid ("MSG ");
OBJ. innertext + = "\ n" + MSG;
}
</SCRIPT>
</Body>
</Html>
Output result:
1111
17
15
F
//////////////////////////////////////// ///////////////////////
Convert to a number:
There are two ways to convert a non-numeric value to a number: parseint () and parsefloat (). These methods can be correctly run only when they are called for the string type; Nan is returned for other types.
Parseint ("1234abc") // returns 1234
Parseint ("0xa") // returns 10
Parseint ("22.5") // returns 22
Parseint ("ABC") // returns Nan
The parseint () method also has the base mode, which can convert binary, octal, hexadecimal, or any other hexadecimal string to an integer.
Parseint ("af", 10) // returns 175
Parsefloat () is similar to parseint (), but there is no base mode.
//////////////////////////////////////// /////////////////////////////////////
Forced type conversion functions: Boolean (value), number (value), string (value ).
Boolean (): returns true if the value to be converted is a string of at least one character, a non-zero number, or an object. If the value is a Null String, number 0, undefined, or null, it returns false.
Number (): it is similar to the processing method of parseint () and parsefloat (), but it converts the entire value instead of the partial value.
Parsefloat ("4.5.6") returns 4.5
Number ("4.5.6") returns Nan
Number (false) returns 0
Number (true) returns 1
Number (undefined) returns Nan
Number (null) returns 0
Number (new object () returns Nan
The only difference between string () and tostring () Is that a string can be generated for the null or undefined value string () without causing an error.
VaR S1 = string (null); // "null"
VaR onull = NULL;
VaR S2 = onull. tostring (); // won't work, causes an error
Reference Type