1. conversion functions:
Js provides two conversion functions: parseInt () and parseFloat. The former converts the value to an integer, and the latter converts the value to a floating point number. The two functions can run correctly only when these methods are called for the String type. NaN (Not a Number) is returned for other types ).
Before determining whether a string is a numeric value, parseInt () and parseFloat () will carefully analyze the string. The parseInt () method first checks the character at the position 0 to determine whether it is a valid number. If not, the method returns NaN and does not continue to perform other operations. However, if the character is a valid number, this method will view the characters at position 1 and perform the same test. This process continues until a non-valid number is found. At this time, parseInt () converts the string before this character into a number.
For example, if you want to convert the string "1234 blue" to an integer, parseInt () returns 1234 because the detection process stops when it detects character B. The number literal in the string is correctly converted to a number, so the string "0xA" is correctly converted to a number 10. However, the string "22.5" is converted to 22 because the decimal point is invalid for integers. Some examples are as follows:
ParseInt ("1234 blue"); // returns 1234
ParseInt ("0xA"); // returns 10
ParseInt ("22.5"); // returns 22
ParseInt ("blue"); // returns NaN
The parseInt () method also has the base mode, which can convert binary, octal, hexadecimal, or any other hexadecimal string to an integer. The base is specified by the second parameter of the parseInt () method. to parse the hexadecimal value, you need to call the parseInt () method as follows:
ParseInt ("AF", 16); // returns 175
Of course, for binary, octal, or even decimal (default mode), you can call the parseInt () method as follows:
ParseInt ("10", 2); // returns 2
ParseInt ("10", 8); // returns 8
ParseInt ("10", 10); // returns 10
If the decimal number contains the leading 0, it is best to use base 10 to avoid unexpected octal values. For example:
ParseInt ("010"); // returns 8
ParseInt ("010", 8); // returns 8
ParseInt ("010", 10); // returns 10
In this Code, both lines of code parse the string "010" into a number. The first line of code regards this string as the octal value. The method for parsing this string is the same as that of the second line of code (The Declaration base is 8. The last line of Code declares that the base number is 10, so iNum3 is equal to 10 at last.
The parseFloat () method is similar to the parseInt () method. Each character is viewed from position 0 until the first invalid character is found, then, convert the string before the character to a number. However, for this method, the first decimal point is a valid character. If there are two decimal points, the second decimal point is considered invalid. The parseFloat () method converts the string before the decimal point to a number. This means that the string "22.34.5" will be parsed to 22.34.
Another difference in using parseFloat () Is that a string must represent a floating point in decimal format, rather than octal or hexadecimal format. The
The method ignores the leading 0, so 0908 of the Octal numbers will be parsed to 908. For the hexadecimal number 0xA, this method returns NaN because x is not a valid character in the floating point number. In addition, parseFloat () does not have a base mode.
The following is an example of using parseFloat:
ParseFloat ("1234 blue"); // 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. Forced type conversion
You can also use type casting to process the type of the converted value. You can use forced type conversion to access a specific value, even if it is another type.
The three types of mandatory conversions available in ECMAScript are as follows:
Boolean (value) -- converts a given value to the Boolean type;
Number (value) -- converts a given value to a Number (which can be an integer or floating point Number );
String (value) -- converts a given value to a String.
Use one of the three functions to convert a value and store the value directly converted from the original value. This will cause unexpected consequences.
When the value to be converted is a string of at least one character, a non-zero number, or an object (This will be discussed in the next section), the Boolean () function returns true. If the value is a null String, number 0, undefined, or null, it returns false.
You can use the following code snippet to test the forced type conversion of the Boolean type.
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 forced type conversion of Number () is similar to that of parseInt () and parseFloat (), except that it converts the entire value instead of the partial value. Do you still remember that the parseInt () and parseFloat () methods only convert strings before the first invalid character, so "4.5.6" will be converted to "4.5 ". Use Number () for forced type conversion. "4.5.6" will return NaN because the entire string value cannot be converted to a Number. If the string value can be fully converted, Number () determines whether to call the parseInt () method or the parseFloat () method. The following table describes how to call the Number () method for different values:
Result through Method
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
No. (100) 100
The last forced type conversion method String () is the simplest, because it can convert any value into a String. To perform this forced type conversion, you only need to call the toString () method of the value passed as the parameter, that is, convert 1 to "1" and convert true to "true ", convert false to "false", and so on. The only difference between forced conversion to a string and calling the toString () method is that forced type conversion to a null or undefined value can generate a string without sending an error:
Var s1 = String (null); // "null"
Var oNull = null;
Var s2 = oNull. toString (); // won't work, causes an error
3. Weak type conversion using js Variables
Let's take a small example. At first glance, we will understand.
<Script>
Var str = '012. 345 ';
Var x = The str-0;
X = x * 1;
</Script>
In the previous example, the weak type of js is used, and only arithmetic operations are performed to convert the string type to the number type. However, this method is not recommended.
// Convert an object to a character
Function object2String (obj ){
Var val, output = "";
If (obj ){
Output + = "{";
For (var I in obj ){
Val = obj [I];
Switch (typeof val ){
Case ("object "):
If (val [0]) {
Output + = I + ":" + array2String (val) + ",";
} Else {
Output + = I + ":" + object2String (val) + ",";
}
Break;
Case ("string "):
Output + = I + ": '" + val + "',";
Break;
Default:
Output + = I + ":" + val + ",";
}
}
Output = output. substring (0, output. length-1) + "}";
}
Return output;
}
Convert an array to a string
Function array2String (array ){
Var output = "";
If (array ){
Output + = "[";
For (var I in array ){
Val = array [I];
Switch (typeof val ){
Case ("object "):
If (val [0]) {
Output + = array2String (val) + ",";
} Else {
Output + = object2String (val) + ",";
}
Break;
Case ("string "):
Output + = "'" + encodeURI (val) + "',";
Break;
Default:
Output + = val + ",";
}
}
Output = output. substring (0, output. length-1) + "]";
}
Return output;
}
Function string2Object (string ){
Eval ("var result =" + decodeURI (string ));
Return result;
}
Function string2Array (string ){
Eval ("var result =" + decodeURI (string ));
Return result;
}
Convert the date type to string format in js
<Script language = "javascript">
/// // E ////// /////////////////
// Obtain the current date, in the format of yyyy-mm-dd
//////////////////////////////////////// ////////////////
Function GetCurrentDate ()
{
Var Year = 0;
Var Month = 0;
Var Day = 0;
Var CurrentDate = new Date ();
Return ChangeDateToString (CurrentDate );
}
/// // E ////// /////////////////
// Obtain the current date in the format of yyyy-mm-dd hh: mm
//////////////////////////////////////// ////////////////
Function GetCurrentTime ()
{
Var Year = 0;
Var Month = 0;
Var Day = 0;
Var CurrentDate = new Date ();
Return ChangeTimeToString (CurrentDate );
}
//////////////////////////////////////// ////////////////
// Convert the date type to the string format yyyy-MM-dd
//////////////////////////////////////// ////////////////
Function ChangeDateToString (DateIn)
{
Var Year = 0;
Var Month = 0;
Var Day = 0;
Var CurrentDate = "";
// Initialization time
Year = DateIn. getYear ();
Month = DateIn. getMonth () + 1;
Day = DateIn. getDate ();
CurrentDate = Year + "-";
If (Month> = 10)
{
CurrentDate = CurrentDate + Month + "-";
}
Else
{
CurrentDate = CurrentDate + "0" + Month + "-";
}
If (Day> = 10)
{
CurrentDate = CurrentDate + Day;
}
Else
{
CurrentDate = CurrentDate + "0" + Day;
}
Return CurrentDate;
}
//////////////////////////////////////// ///////////////
// Convert the date type to the string format yyyy-MM-dd hh: mm
//////////////////////////////////////// ////////////////
Function ChangeTimeToString (DateIn)
{
Var Year = 0;
Var Month = 0;
Var Day = 0;
Var Hour = 0;
Var Minute = 0;
Var CurrentDate = "";
// Initialization time
Year = DateIn. getYear ();
Month = DateIn. getMonth () + 1;
Day = DateIn. getDate ();
Hour = DateIn. getHours ();
Minute = DateIn. getMinutes ();
CurrentDate = Year + "-";
If (Month> = 10)
{
CurrentDate = CurrentDate + Month + "-";
}
Else
{
CurrentDate = CurrentDate + "0" + Month + "-";
}
If (Day> = 10)
{
CurrentDate = CurrentDate + Day;
}
Else
{
CurrentDate = CurrentDate + "0" + Day;
}
If (Hour> = 10)
{
CurrentDate = CurrentDate + "" + Hour;
}
Else
{
CurrentDate = CurrentDate + "0" + Hour;
}
If (Minute> = 10)
{
CurrentDate = CurrentDate + ":" + Minute;
}
Else
{
CurrentDate = CurrentDate + ": 0" + Minute;
}
Return CurrentDate;
}
</Script>