Subtracting long numbers in javascript

Because numbers in JavaScript are floating-point. They have limited precision.

When JavaScript sees a very long number, it rounds it to the nearest number it can represent as a 64-bit float. In your script, start and end get rounded to the same value.

alert(1234567890123456789);   // says: 1234567890123456800
alert(1234567890123456799);   // says: 1234567890123456800

There’s no built-in way to do precise arithmetic on large integers, but you can use a BigInteger library such as this one.

Leave a Comment