admin管理员组文章数量:1429861
I am trying to pull a number (72157648141531978), which starts at the 21st character, out of the title of a page like so:
parseInt(document.title.substring(21), 10);
This returns the string as an integer of 72157648141531980. I can't seem to figure out why it is changing the last two numbers. Any help would be appreciated.
I am trying to pull a number (72157648141531978), which starts at the 21st character, out of the title of a page like so:
parseInt(document.title.substring(21), 10);
This returns the string as an integer of 72157648141531980. I can't seem to figure out why it is changing the last two numbers. Any help would be appreciated.
Share Improve this question asked Oct 9, 2014 at 4:54 metalfoleymetalfoley 535 bronze badges 3- What's the full title string? – Alfredo Delgado Commented Oct 9, 2014 at 4:57
- @Sebas - Oh I see it now. – Derek 朕會功夫 Commented Oct 9, 2014 at 5:05
-
2ality./2013/01/parseint.html, interesting articles,
all numbers are floating points in javascript
mind blown! – Sebas Commented Oct 9, 2014 at 5:07
2 Answers
Reset to default 7According to What is JavaScript's highest integer value that a Number can go to without losing precision? the max value of an integer is 9007199254740992.
I tried your calculation on http://www.w3schools./jsref/tryit.asp?filename=tryjsref_parseint and I can confirm your problem.
It looks like an issue parsing beyond this max value and it is rounding the last 2 figures.
You have exceeded the limits of double-precision floating-point format, as used by JavaScript. You cannot use that precise number directly in JavaScript. You can use it as a string, but if you need to do arithmetic on it you will need a bignum library.
本文标签: javascriptparseInt changes the integerStack Overflow
版权声明:本文标题:javascript - parseInt changes the integer - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745554873a2663124.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论