Javascript long integer

前端 未结 3 1582
长情又很酷
长情又很酷 2020-11-27 06:19

I have seen one of the weirdest things in javascript. The server side (spring):

   @RequestMapping(value = \"/foo\", method = RequestMethod.GET)
   @Response         


        
相关标签:
3条回答
  • 2020-11-27 06:33

    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>
    
    0 讨论(0)
  • 2020-11-27 06:40

    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.

    0 讨论(0)
  • 2020-11-27 06:46

    You can use long npm package.

    It let you have 64 bits integers in JavaScript.

    You can also use BigInt JavaScript built-in method.

    0 讨论(0)
提交回复
热议问题