0

Here is the code:

var q = 10000000000000011;
console.log(q);

Output will be:

10000000000000012

If I try to output 10000000000000010 or 10000000000000012, everything is fine. Conversion to string doesn't help either. How can I avoid this bug?

2
  • Javascript can not represent such big numbers accurately - instead it will convert to closest even number. There are various libraries for handling big numbers. Commented Aug 29, 2017 at 10:08
  • Thank you guys for quick response. Link which helped me with the case github.com/MikeMcl/big.js Commented Aug 29, 2017 at 10:23

2 Answers 2

4

The maximum safe integer in JavaScript (2^53 - 1). which is 9007199254740991. You will need to use a big integer library to store such large numbers

Sign up to request clarification or add additional context in comments.

2 Comments

Number.MAX_SAFE_INTEGER
I'll leave this here, someone might find it useful ... or not.. ecma262-5.com/ELS5_HTML.htm#Section_8.5
0

As has been pointed out, the maximum integer size in JS is Number.MAX_SAFE_INTEGER (9007199254740991)

So if you want larger numbers, you'll have to get creative and use something like scientific notation:

10000000000000011 ~= 1 × 10^9 (1e9)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.