Arthur
Arthur

Reputation: 3506

Convert string to integer BigInt

This may be a recurring question. I need to convert a string to an integer. But JS does this: parseInt("2166767952358020110") ⇒ 2166767952358020000

I know why it happens but how to correctly convert a string to an integer? BigInt() doesn't fit in my case.

Upvotes: 1

Views: 1187

Answers (1)

oktapodia
oktapodia

Reputation: 1758

As your number is above the Number.MAX_SAFE_INTEGER, you can't directly convert the string to a number without having some errors.

I suggest you to use the BigNumber library which has been done for this purpose

const BN = require('bn.js');

const number = new BN('2166767952358020110', 10);

Upvotes: 1

Related Questions