parseint (string, radix)
function has two ways to call
1, the designation radix, this is also the recommended way, unfortunately I have not done this before
2, do not specify Radix, namely: parseint (String). Although simple, but a lot of rules, this is the core of the content of this article.
parseint (string, radix)
Radix, which represents the base of the conversion, which we often say 2, 8, 10, 16 and so on. range from 2~36, but we generally call this method in JS, the basic is based on 10 of the conversion.
If the argument is less than 2 or greater than 36, parseint () returns NaN.
The detailed rules for the "string" parameter apply are as follows
(Radix 10 is an example because it occurs most frequently when it is actually used.) And the use of other cardinality, you also need to be very clear about the conversion between the system, this should be the computer professional key courses, if forgotten, the use of this method will be very "uncomfortable"):
1 if all are letters, return: NaN, example:
The code is as follows |
Copy Code |
<script type= "Text/javascript" > Window.onload = function Testparse () { Alert (parseint ("abc", 10)); } </SCRIPT>2) If they are not letters, return: 123, Example: <script type= "Text/javascript" > Window.onload = function Testparse () { Alert (parseint ("123", 10)); } </script> 3) |
If both letters and numbers exist, for example:
The code is as follows |
Copy Code |
<script type= "Text/javascript" > Window.onload = function Testparse () { Alert (parseint ("1X2BC", 10)); Alert (parseint ("DF2BC", 10)); } </script>parseint ("1X2BC", 10) return: 1
parseint ("DF2BC", 10) |
Back: NaN
Here are two rules involved:
3.1 If the argument "string" begins with a number, it converts all numbers before the first letter appears (NND, always feeling the description is not in place).
In the example above, the first letter that appears is ' X ', takes the number string before it, has only one digit ' 1 ', and returns 1.
3.2 If the argument "string" begins with a letter, it returns Nan directly (because the letter in the 10 binary is not a valid representation, what if Radix is 16?). As a result, everyone can run on their own. )
The above description is explained according to ECMAScript (official translation, very power):
The parseint () method first looks at the character at position 0 to determine whether it is a valid number, and if not, the method returns NaN and does not continue with other operations. However, if the character is a valid number, the method will look at the characters at position 1 and do the same test. This process continues until a character is found that is not a valid number, at which point parseint () converts the string before the character to a number.
parseint (String,radix) introduced to this, if there are wrong or missing, please reader to add, in this thanked the
parseint (String)
When this function is called in this way, there are many "unspoken rules" because the specified radix is not displayed.
Here are two paragraphs of references
W3school
When the value of the parameter radix is 0, or if the parameter is not set, parseint () determines the cardinality of the number based on a string.
For example, if a string starts with "0x", parseint () resolves the remainder of the string to an integer of 16. If a string starts with 0, then ECMAScript V3 allows an implementation of parseint () to parse the subsequent character into octal or hexadecimal digits. If a string starts with a number 1 ~ 9, parseint () resolves it to a decimal integer.
Mozilla developer
if the ' input string begins with ' 0x ' or ' 0X ', radix is (hexadecimal) and the remainder of the ' string is parsed.
if the input string begins with "0" and Radix is eight (octal) or (decimal). Exactly which Radix is chosen is implementation-dependent. ECMAScript 5 Specifies that's used, but not all browsers support the this yet. For this reason always specify a radix when using parseint.
if the input string begins with any other value and the Radix is (decimal).
If the character cannot is converted to a number, parseint returns NaN.