JS number function adds zeros at the end

I am using the Number() JS function, which should convert a string value to a numeric value.

It works great for small numbers. For large - the substitution value starts with zeros, as shown in the image:

enter image description here

Is there a problem for this problem?

+7
javascript
source share
2 answers

In JS, the largest integer value is 9007199254740991 That is, all positive and negative integers should not exceed -9007199254740991 and 9007199254740991 respectively.

The same is defined as 2 53 -1.

 console.log(Number.isSafeInteger(parseInt('1111111111'))) console.log(parseInt('1111111111')) console.log(Number.isSafeInteger(parseInt('111111111111111111'))) console.log(parseInt('111111111111111111')) //9007199254740991 - The largest JS Number console.log(Number.isSafeInteger(parseInt('9007199254740991'))) 
+2
source share

This is because you are using numbers that are greater than Number.MAX_SAFE_INTEGER , and Javascript does not guarantee the correct presentation of these numbers

Use Number.isSafeInteger to check:

 > Number.isSafeInteger(Number('111111111111111111')) < false 
+2
source share

All Articles