parseInt changes the integer

2019-05-26 01:15发布

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.

2条回答
\"骚年 ilove
2楼-- · 2019-05-26 01:38

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.

查看更多
戒情不戒烟
3楼-- · 2019-05-26 02:00

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.

查看更多
登录 后发表回答