0

When converting a large string to a number, I noticed there is round after the 15th digit. Is there a way to convert a string to a number to in JS without rounding, regardless of the length?

Number("9223372036854775807")
// returns  9223372036854776000

+"9223372036854775807"
// returns  9223372036854776000

"9223372036854775807"*1
// returns  9223372036854776000
eagercoder
  • 360
  • 7
  • 22

1 Answers1

1

You can use the BigInt object which can hold numbers upto 2^53 - 1.

let hugeString = BigInt("9223372036854775807")
console.log(hugeString);
// outputs: 9223372036854775807n
Vijay Joshi
  • 881
  • 8
  • 17