admin管理员组文章数量:1335442
I am not able to understand what is the use of second parameter
of the parseInt
method in JavaScript. Below are some of the outputs:
parseInt("9",10)
====> Output: 9
parseInt("9",100)
====> Output: NaN
parseInt("90",10)
====> Output: 9
parseInt("90",100)
====> Output: NaN
Kindly explain what is the use of the second parameter.
I am not able to understand what is the use of second parameter
of the parseInt
method in JavaScript. Below are some of the outputs:
parseInt("9",10)
====> Output: 9
parseInt("9",100)
====> Output: NaN
parseInt("90",10)
====> Output: 9
parseInt("90",100)
====> Output: NaN
Kindly explain what is the use of the second parameter.
Share Improve this question edited Dec 31, 2013 at 1:56 Antony 15.1k10 gold badges47 silver badges76 bronze badges asked Dec 31, 2013 at 1:55 user182944user182944 8,06736 gold badges111 silver badges182 bronze badges 2- 2 Documentation is your friend. – Antony Commented Dec 31, 2013 at 1:57
- Check the docs. The second parameter is the radix, which you should always specify. developer.mozilla/en-US/docs/Web/JavaScript/Reference/… – Palpatim Commented Dec 31, 2013 at 1:57
6 Answers
Reset to default 3The second paramater is known as the Radix -- see here http://en.wikipedia/wiki/Radix, which is used to specify the numbering system. See here for a more detailed explanation http://mir.aculo.us/2010/05/12/adventures-in-javascript-number-parsing/
It's the base. So if it's 10, it operates as normally.
parseInt("9",8) ===> NaN
parseInt("7",8) ===> 7
Docs are here: https://developer.mozilla/en-US/docs/Web/JavaScript/Reference/Global_Objects/parseInt
The radix is used to specify the range of allowed numerals, also known as the number system or base; when omitted, it guesses the value by looking at the prefix of your string, e.g. a "0x" prefix means radix of 16 (hexadecimal). It's a good practice to always set this explicitly to 10
for the decimal system.
When a numeral is outside of the specified radix, parseInt()
will return NaN
.
As for the NaN
result you're seeing with a radix of 100, though not documented per se, the allowed range of the radix is [1, 36], i.e. the largest base prises digits and alphabets.
Quoting the MDN (https://developer.mozilla/en-US/docs/Web/JavaScript/Reference/Global_Objects/parseInt):
radix
An integer that represents the radix of the above mentioned string. Always specify this parameter to eliminate reader confusion and to guarantee predictable behavior. Different implementations produce different results when a radix is not specified.
Think of it like the base of the number you try to parse, it will return the number in base 10:
parseInt(11,10) // -> 11
parseInt(11,2) //-> 3 (binary)
parseInt(1,6) // 1
parseInt(2,6) // 2
parseInt(3,6) // 3
parseInt(4,6) // 4
parseInt(5,6) // 5
parseInt(6,6) // NaN
parseInt(7,6) // NaN
parseInt(8,6) // NaN
parseInt(9,6) // NaN
parseInt(10,6) // 6, since 5 is the highest digit in base 6 and because of this after 5 es 10
The 2nd parm is the radix (or base number). The defaut is base 10, so if you omit it, or use 10, then you'll get the same decimal number that you're parsing. If you specify base 16 (hex), then the parsed number is calculated thusly:
Examples:
parseInt("1",16) = 1
parseInt("2",16) = 2
parseInt("3",16) = 3
...
parseInt("9",16) = 9
parseInt("A",16) = 10
parseInt("B",16) = 11
...
parseInt("F",16) = 15
parseInt("10",16) = 16 (1x16 + 0)
parseInt("11",16) = 17 (1x16 + 1)
parseInt("12",16) = 18 (1x16 + 2)
Does that help?
first, some of your example is incorrect. parseInt("90",10) should be output 90.
the second parameter is ary. parseInt("90",10) means parse "90" as decimal.
but why parseInt(xxx, 100) output NaN? You should try parseInt("90", 36) and parseInt("90", 37).
Understand? there are only 36 letter so that no more letter to display the number witch in 37 hex.
finally, sorry about my english.
本文标签: parseInt method in JavaScriptStack Overflow
版权声明:本文标题:parseInt method in JavaScript - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742378460a2463633.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论