Article Introduction: The parseint you don't know. |
I believe we all know parseint () is to do what, here is not long-winded. Previously used parseint () is always parseint (ARG), remember in a JavaScript book read, he can have a second argument, but never used, until today, I see two lines of code: parseint ("08"); 0 parseint ("08", 10); 8
Found, do not understand why, so spent one hours of thin research, the original parseint is a sample drop ~ Of course there are two situations:
A. parseint (ARG)------takes only one parameter, and the second argument omits the case(1) parseint (' a ')------at this time omit the second parameter, the default is decimal, so ' 12 ' is expressed as a decimal (2) parseint (' 012 ')------' The second argument is omitted here, but ' 012 ' begins with ' 0 ', So the default is 8 (3) parseint (' 0x12 ')------' The second argument is omitted, but ' 012 ' starts with ' 0x ', so the default is 16.
two. parseint (arg1, arg2)------Arg1 is the number to be manipulated, arg2 is the number of numbers of arg1(1) parseint (' A ', 8)-----means that here ' 12 ' is a 8 (2) parseint (' $ ')-----means that here ' 78 ' is 10 (3) parseint (' 012 ', ten)-----here though ' 012 ' Is the first drop, the default is 8, but the second argument is forced to tell the computer ' 012 ' is a 10 number
Well, after you know the above points, let's look at the following example:
1. Alert (parseint ('), 8);//10Solution: Converts the 8 ' 12 ' to decimal: 1*8 (8 of the 1 power) + 2* (8 power) = 0;
2. Alert (parseint (' 012 '));//10Solution: Here ' 012 ' starts with ' 0 ', so it's 8. The 8 into the ' 012 ' conversion to decimal, are the same, but there may be some people think, and it also omitted the second parameter, why not the decimal ' 012 ', here I do not know, the program test results of 10, I guess it was detected with the ' 0 ' beginning to indicate a 8-binary ratio omit the second parameter default is 10 in the first stage of the high bar, welcome to know the heroes pointing
3. Alert (parseint (' 012 ', 8));Solution: The situation should be very good to understand, itself ' 012 ' By default is 8, followed by the second parameter to emphasize 8
4. Alert (parseint (' 012 '));Solution: This kind of situation ' 012 ' By default is 8, but the second argument tells the computer that the first number is actually 10, so the ' 012 ' when the 10 processing results 12
5. Alert (parseint ('//7 ', 8));Solution: Represents the 8 binary ' 78 ' calculation parseint, because 8 is not possible in the 8 binary, so the first digit 7 is directly intercepted and 7*8 (8 power of the exponent) =7
6. Alert (parseint (' 8));//0Solution: here with the 5th
7. Alert (parseint (' a) ");//9Solution: Although this is ' 08 ' by default is 8, but the second argument is forced to 10, so the result 9
8. Alert (parseint ('));//0Solution: here 8 binary ' 08 ' only intercepts ' 0 ' operation
9. Alert (parseint (' 8 ', 8));//nanSolution: Because 8 is not possible in 8 in-system, direct Nan
Alert (parseint (' 0x12 '));//18Solution: Here ' 0x12 ' is started with ' 0x ', so the default is 16, and the 16-in ' 12 ' converts to 10 to 18
Well, that's what I understand, what's wrong with the word, welcome to the brick, gently oh