Transformation Function number () can be used for any data type, while the other two functions are specifically used to convert strings into numerical values. 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