ad1tya2
ad1tya2

Reputation: 35

Entering an very large integer results in a rounded number node js

The code

console.log(791991727573303311)

Expected result 791991727573303311

Actual result791991727573303300

How do i solve this issue without resorting to using strings.

And

Does this only affect printing the values?

Upvotes: 0

Views: 137

Answers (1)

Ian
Ian

Reputation: 461

The issue is that you're going over the Number.MAX_SAFE_INTEGER constant. Which is basically the limit to which a Number is accurate, anything above that and javascript will start to round the number as in your example.

To solve this you can use BigInt which supports any size of number.

Additional information can be found here: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/BigInt

Upvotes: 2

Related Questions