I have an issue where a string containing a 64 bit number, is being parsed by parseInt
and some bits are lost in the process. my application uses ES5 and so BigInt
is not available. How can I parse it as a number of 64 bit then under ES5?
str = "72057594044342942"
console.log(parseInt(str));
2
Answers
If you want to introduce a new function into an older version of software, we apply so-called polyfills for that purpose.
You can also find such a thing for BigInt on the internet.
big-integer (npmjs.com)
In ECMA5, without BigInt, handling 64-bit numbers precisely is tricky. Since
parseInt
loses precision with such large numbers, consider using a library likebignumber.js
ordecimal.js
. These libraries can handle large integers safely, even in environments without BigInt support. Just replaceparseInt
with the appropriate method from the library you choose. This workaround maintains precision for your 64-bit numbers.