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
Add a ment  | 

2 Answers 2

Reset to default 7

According 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