2

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.

metalfoley
  • 33
  • 5

2 Answers2

5

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.com/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.

Community
  • 1
  • 1
Guy Lowe
  • 2,115
  • 1
  • 27
  • 37
0

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.

Boann
  • 48,794
  • 16
  • 117
  • 146