Javascript long integer Javascript long integer spring spring

Javascript long integer


In Java, you have 64 bits integers, and that's what you're using.

In JavaScript, all numbers are 64 bits floating point numbers. This means you can't represent in JavaScript all the Java longs. The size of the mantissa is about 53 bits, which means that your number, 793548328091516928, can't be exactly represented as a JavaScript number.

If you really need to deal with such numbers, you have to represent them in another way. This could be a string, or a specific representation like a digit array. Some "big numbers" libraries are available in JavaScript.


May be late, but definitely helps others who run this situation first time.

I too wanted some calculation on large numbers in JavaScript. There are lot of libraries available to deal with such numbers. But most of them may waste lot of your time.Here, https://github.com/peterolson/BigInteger.js, is a direct, complete(for non node environment and node JS environments), and working solution for all operations on large numbers available. or find the following simple and sample working HTML code snippet which calculates modulo/remainder,

<script src="http://peterolson.github.com/BigInteger.js/BigInteger.min.js"></script><script type="text/javascript">    function modTest(){       var rem = bigInt("1738141852226360940").mod("32").valueOf();       console.log(rem);       document.getElementById("remainder").innerHTML = rem;    }</script><BODY onload="modTest();">    <p id="remainder"></p></BODY>


You can use long npm package.

It let you have 64 bits integers in JavaScript.

You can also use BigInt JavaScript built-in method.