Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
379 views
in Technique[技术] by (71.8m points)

javascript - 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?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...