'Convert a string to number with parseInt does not work as expected [duplicate]

A quick maybe simple question :

We are trying to parse a string into number , the sample number is "1928433000460244141" so when we try to parse it to integer we get 1928433000460244200

const no1 = "1928433000460244141" ;
console.log(parseInt(no1)); // returns 1928433000460244200

what can cause this problem and what is the solution ?

BigInt can be used to store the data , but the problem is we want to send the string converted to number to a service we are using right now , we do not have any access hence it is a 3rd party service so we should handle it from our side.



Solution 1:[1]

The MAX_SAFE_INTEGER constant has a value of 9007199254740991 (9,007,199,254,740,991 or ~9 quadrillion). The reasoning behind that number is that JavaScript uses double-precision floating-point format numbers as specified in IEEE 754 and can only safely represent integers between -(2^53 - 1) and 2^53 - 1.

Source

Because your number is larger than the safe integer limit, you're likely experiencing some floating-point precision issues that are rounding the value off.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Dave Meehan