Convert JavaScript parseInt buggy

At startup

console.log(parseInt("9658921879781125")) 
it gives a value of 9658921879781124 , which is less than the original value.

Why is this so?

+7
javascript
source share
1 answer

That would be because

 9658921879781125 > Number.MAX_SAFE_INTEGER // true 

Therefore, it is unsafe to try to work with numbers larger than Number.MAX_SAFE_INTEGER


The constant MAX_SAFE_INTEGER has the value 9007199254740991 (9 007 199 244 740 991 or ~ 9 quadrillion). The rationale for this number is that JavaScript uses double-precision floating-point numbers, as specified in IEEE 754, and can only safely represent numbers between - (2 53 - 1) and 2 53 - 1.

Safe in this context refers to the ability to accurately represent integers and correctly compare them .

+4
source share

All Articles