0

I have a string as numbers. And I want to transform string to int.

So my code like that:

const bigNumber = '6972173290701864962'
console.log(bigNumber)
//6972173290701864962         =====> last digits : *****1864962 

console.log(Number(bigNumber))
//6972173290701865000         =====> last digits : *****1865000

Why Im getting rounding number? How can I solve this problem?

akasaa
  • 1,282
  • 4
  • 13
  • 33

1 Answers1

2

The number is greater than Number.MAX_SAFE_INTEGER.

Instead, cast a to a BigInt:

const bigNumber = '6972173290701864962'
console.log('Number: '+Number(bigNumber))
console.log('BigInt: '+BigInt(bigNumber))

To remove the trailing n, simply call toString():

const bigNumber = '6972173290701864962'
console.log(BigInt(bigNumber).toString());
Spectric
  • 30,714
  • 6
  • 20
  • 43