-1

When I tried BigInt, the result returned a wrong result:

BigInt(123456789123456789*111111111111)
13717421013703702653171662848n

And this is what the actual result should be like by hand-written calculation:

13717421013703703578986282579

Is there a way to produce the correct result without this error? Thank you.

Sebastian Simon
  • 18,263
  • 7
  • 55
  • 75
Tony Tsui
  • 21
  • 3
  • `BigInt("123456789123456789") * BigInt("111111111111")` or `123456789123456789n * 111111111111n`. `123456789123456789 * 111111111111` just results in a regular, imprecise number. – Sebastian Simon Jul 27 '20 at 03:31

1 Answers1

2

Instead of passing the multiplication to BigInt, convert both numbers to BigInt and multiply them after that:

const big = 123456789123456789n * 111111111111n;
console.log(big)

Note, use your browser's console, not the snippet's one (also, this won't work in Safari).

  • You could add a `toLocaleString`, if you need the full string version / grouped version of the number. > (123456789123456789n*111111111111n).toLocaleString('fullwide', {useGrouping:false}) – Vasanth Gopal Jul 27 '20 at 03:43