This is the fifth day of my participation in the August Wen Challenge.More challenges in August
Calculation rules:
parseIntThe () function parses a string and returns an integer.parseInt(String, radix) String: necessary. String to be parsed. Radix: Optional. Represents the radix of the number to be parsed. This value is between2 ~ 36In between. If this parameter is omitted or its value is0, the number will be10To analyze on the basis of. If it starts with "0x" or "0x", it will be16As the base. If the parameter is less than2Or greater than36,parseInt() will returnNaN.Copy the code
In short, it is the conversion of the following base
Binary data can generally be written as: Rule: The degree of the digits in the ones place is0The power of the tens place is zero1. , and the degree of tenths is minus1The number of hundredths is minus2. In descending order.Copy the code
For example:
A parameter when:
When a parameter is based on the form of the parameter to determine how many base
parseInt("26"); // The default decimal value
parseInt("010") // A decimal number starting with 0
parseInt("0x10") // A hexadecimal number starting with 0x
Copy the code
The results are as follows:
parseInt("26"); When converting to the default decimal:2*Math.pow(10.1) +6*Math.pow(10.0)
Copy the code
With two parameters:
The calculation method is as shown in the figure above:
parseInt("11".4); / / 5
1*Math.pow(4.1) +1*Math.pow(4.0) / / 5
Copy the code
But when the radix of the later radix is less than a single number of the previous string, the following: the previous number greater than the radix and all numbers after that number are ignored
parseInt("152".4);/ / 1
1*Math.pow(4.0) / / 1
parseInt("125".4); / / 6
1*Math.pow(4.1) +2*Math.pow(4.0) / / 6
Copy the code
You can refer to the implementation code in the following article, but “0”, as shown below, should also be a decimal conversion from the console output.
Refer to the link
Other implementations
1. Favor the js weakly typed language way “-” to carry out the conversion
2. Use ASCII codes for conversion
At present, I can only think of these schemes. If you have other schemes, please leave a message ~~~~