There are three functions that can convert non-numeric values into numeric values: Number (), parseInt (), and parseFloat (). The first function, that is, the transformation function Number (), can be used for any data type, and the other two functions are specifically used to convert a string to a value. These three functions have different results for the same input.
The conversion rules of the Number () function are as follows:
If it is a Boolean value, true and false are converted to 1 and 0 respectively.
If it is a numeric value, it is simply passed in and returned
If the value is null, 0 is returned.
If it is undefined, NaN is returned.
If it is a string, follow the following rules:
If a string contains only numbers, it is converted to a decimal value. Level 1 is converted to 1, and "123" is converted to 123, "011" will change to "11" (the previous 0 is ignored)
If the string contains only valid floating point formats, such as "1.1", it is converted to the corresponding floating point value (similarly, leading zero is ignored)
If the string contains only valid hexadecimal format, for example, "0xf", convert it to a decimal integer of the same size.
If the string is null (does not contain any characters), convert it to 0.
If the string contains characters other than the preceding format, convert it to NaN
If it is an object, call the valueOf () method of the object and convert the returned value according to the previous rules. If the conversion result is NaN, it is a bit complicated to call the toString () of the object to convert various data types to numerical values. The following are examples:
Var num1 = Number ("Hello world! "); // NaN
Var num2 = Number (""); // 0
Var num3 = Number ("000011"); // 11
Var num4 = Number ("true"); // 1
First, the string "Hello world !" It is converted to NaN because it does not contain any meaningful numeric value. The empty string is converted to 0. The string "000011" is converted to 11 because leading zero is ignored. Finally, the true value is converted to 1.
Because the Number () function is complex and not reasonable enough to convert strings, parseInt () is more commonly used when processing integers. When converting a string, parseInt () determines whether it complies with the numeric mode. It ignores spaces before the string until the first non-space character is found. If the first character is not a numeric character or a negative Number, parseInt () returns NaN; that is, if you use parseInt () to convert an empty string, NaN (Number () is returned () returns 0 for a Null String ). If the first character is a numeric character, parseInt () will continue to parse the second character until all subsequent characters are parsed or a non-numeric character is encountered. For example, "123blue" is converted to 1234 because "blue" is ignored completely. Similarly, "22.5" is converted to 22 because the decimal point is not a valid numeric character.
If the first character in a string is a numeric character, parseInt () can also recognize various integer formats (decimal, octal, and hexadecimal ). That is to say, if the string starts with "0x" and is followed by a number character, it will be treated as a hexadecimal integer. If the string starts with "0" and is followed by a number character, it is parsed as an octal number.
To better understand the conversion rules of parseInt () functions, the following examples are provided:
Var num1 = parseInt ("1234 blue"); // 1234
Var num2 = parseInt (""); // NaN
Var num3 = parseInt ("0xA") // 10 (hexadecimal)
Var num4 = parseInt ("22.5"); // 22
Var num5 = parseInt ("070"); // 56 (octal)
Var num6 = parseInt ("70"); // (70) decimal
Var num7 = parseInt ("0xF") // 15 (hexadecimal)
When understanding these examples, the most important thing is to note that parseInt () resolves the different methods of "070" and "70. In this case, the leading zero in "070" indicates that this is an octal (rather than decimal) string, and the result is 56 (note that this result is different from calling the Number () function ). But "70" is converted to 70 because there is no leading zero. To eliminate the above confusions that may occur when you use the parseInt () function, ECMAScript also provides the second parameter for this function: The base number used for conversion (that is, the number of hexadecimal values ).
If you want to know that the value to be parsed is a string in hexadecimal format, specify base 16 as the second parameter to obtain the correct result. For example:
Var num = parseInt ("0xAF", 16); // 175
In fact, if 16 is specified as the second parameter, the string may not contain the preceding "0x", as shown below:
Var num1 = parseInt ("AF", 16); // 175
Var num2 = parseInt ("AF"); // NaN
In this example, the first one is successfully converted, and the second one fails. The difference is that the first conversion passes in the base number and clearly tells parseInt () to parse a string in hexadecimal format. The second conversion finds that the first character is not a numeric character, therefore, it is automatically terminated.
Specifying the Base will affect the output result of the conversion. For example:
Var num1 = parseInt ("10", 2); // 2
Var num2 = parseInt ("10", 8); // 8
Var num3 = parseInt ("10", 10); // 10
Var num4 = parseInt ("10", 16); // 16
Because no base number is specified, parseInt () is used to determine how to parse the input string. To avoid incorrect parsing, we recommend that you specify the base no matter under any circumstances-especially in the case of processing the gossip as follows:
Var num1 = parseInt ("010"); // 8
Var num2 = parseInt ("010", 8); // 8
Var num2 = parseInt ("010", 10); // 10
In this example, "010" is converted to different values because the second parameter is different. The first line of conversion is intuitive, that is, let parseInt () decide how to convert. Since the first character is "0" and followed by a number, parseInt () is assumed to be an octal number. In fact, the second line of the default behavior domain of parseInt () indicates that the base behavior is consistent. In the third row, the base number 10 is input. Therefore, parseInt () ignores the leading zero in the string and only parses the remaining digit characters.
In most cases, all values are parsed in decimal format. Therefore, it is necessary to always use 10 as the second parameter.
Similar to the parseInt () function, parseFloat () parses each character from the first character (position 0. It is resolved to the end of the string, or to an invalid floating-point numeric character. That is to say, the first decimal point in the string is valid, and the second decimal point is invalid. Therefore, the string after it is ignored. For example, "22.34.5" will be converted to 22.34.
Except that the first decimal point is valid, the second difference between parseFloat () and parseInt () is that it always ignores the leading zero. ParseFloat () can recognize all the floating point numeric formats discussed earlier, including the decimal integer format. However, the string in hexadecimal format is always converted to 0. Since parseFloat () Only parses the decimal value, it does not use the second parameter to specify the base number. Note: If the string contains a number that can be parsed as an integer (no decimal point or zero after the decimal point), parseFloat () returns an integer. The following are typical examples of using parseFloat () to convert values:
Var num1 = parseFloat ("1234 blue"); // 1234
Var num1 = parseFloat ("0xA"); // 0
Varnum1 = parseFloat ("22.5"); // 22.5
Var num1 = parseFloat ("22.34.5"); // 22.34
Varnum1 = parseFloat ("0908.5"); // 908.5
Var num1 = parseFloat ("3.125e7"); // 31250000
From the dark day, how do you understand the dark night?