Home > Enterprise >  Js return wrong sum result
Js return wrong sum result

Time:10-05

In JS 9007199254740992 1 should be 9007199254740993.

I'm getting 9007199254740992.

What's going on here?

console.log(9007199254740992   1)
// 9007199254740992

CodePudding user response:

You can use BigInts to prevent overflow like this.

console.log((9007199254740992n   1n).toString())

Or, if it's a variable:

const num = "9007199254740992";
console.log((BigInt(num)   1n).toString())

CodePudding user response:

Try to shrink the number, Javascript has a hard time processing large values. Or if you don't want to, turn the big number into a value.

const bigNum = 9007199254740992;
console.log(bigNum   1);
  • Related