Issue with combining large array of numbers into one single number





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}







1















I am trying to convert an array of numbers into one single number, for example



[1,2,3] to 123. 


However, my code can't handle big arrays since it can’t return exact number. Such as



[6,1,4,5,3,9,0,1,9,5,1,8,6,7,0,5,5,4,3] returns 6145390195186705000


Is there any way that I could properly convert into a single number.I would really appreciate any help.



var integer = 0;
var digits = [1,2,3,4]
//combine array of digits into int
digits.forEach((num,index,self) => {
integer += num * Math.pow(10,self.length-index-1)
});









share|improve this question




















  • 1





    Use BigInt libraries or save them as string and manipulate them as number

    – quirimmo
    Jan 3 at 9:09











  • Check out enter link description here. You number is higher than max safe integer in js

    – deathangel908
    Jan 3 at 9:09











  • Possible duplicate of What is JavaScript's highest integer value that a number can go to without losing precision?

    – Peter B
    Jan 3 at 9:12


















1















I am trying to convert an array of numbers into one single number, for example



[1,2,3] to 123. 


However, my code can't handle big arrays since it can’t return exact number. Such as



[6,1,4,5,3,9,0,1,9,5,1,8,6,7,0,5,5,4,3] returns 6145390195186705000


Is there any way that I could properly convert into a single number.I would really appreciate any help.



var integer = 0;
var digits = [1,2,3,4]
//combine array of digits into int
digits.forEach((num,index,self) => {
integer += num * Math.pow(10,self.length-index-1)
});









share|improve this question




















  • 1





    Use BigInt libraries or save them as string and manipulate them as number

    – quirimmo
    Jan 3 at 9:09











  • Check out enter link description here. You number is higher than max safe integer in js

    – deathangel908
    Jan 3 at 9:09











  • Possible duplicate of What is JavaScript's highest integer value that a number can go to without losing precision?

    – Peter B
    Jan 3 at 9:12














1












1








1








I am trying to convert an array of numbers into one single number, for example



[1,2,3] to 123. 


However, my code can't handle big arrays since it can’t return exact number. Such as



[6,1,4,5,3,9,0,1,9,5,1,8,6,7,0,5,5,4,3] returns 6145390195186705000


Is there any way that I could properly convert into a single number.I would really appreciate any help.



var integer = 0;
var digits = [1,2,3,4]
//combine array of digits into int
digits.forEach((num,index,self) => {
integer += num * Math.pow(10,self.length-index-1)
});









share|improve this question
















I am trying to convert an array of numbers into one single number, for example



[1,2,3] to 123. 


However, my code can't handle big arrays since it can’t return exact number. Such as



[6,1,4,5,3,9,0,1,9,5,1,8,6,7,0,5,5,4,3] returns 6145390195186705000


Is there any way that I could properly convert into a single number.I would really appreciate any help.



var integer = 0;
var digits = [1,2,3,4]
//combine array of digits into int
digits.forEach((num,index,self) => {
integer += num * Math.pow(10,self.length-index-1)
});






javascript arrays






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jan 3 at 9:12







user545871

















asked Jan 3 at 9:06









user545871user545871

626




626








  • 1





    Use BigInt libraries or save them as string and manipulate them as number

    – quirimmo
    Jan 3 at 9:09











  • Check out enter link description here. You number is higher than max safe integer in js

    – deathangel908
    Jan 3 at 9:09











  • Possible duplicate of What is JavaScript's highest integer value that a number can go to without losing precision?

    – Peter B
    Jan 3 at 9:12














  • 1





    Use BigInt libraries or save them as string and manipulate them as number

    – quirimmo
    Jan 3 at 9:09











  • Check out enter link description here. You number is higher than max safe integer in js

    – deathangel908
    Jan 3 at 9:09











  • Possible duplicate of What is JavaScript's highest integer value that a number can go to without losing precision?

    – Peter B
    Jan 3 at 9:12








1




1





Use BigInt libraries or save them as string and manipulate them as number

– quirimmo
Jan 3 at 9:09





Use BigInt libraries or save them as string and manipulate them as number

– quirimmo
Jan 3 at 9:09













Check out enter link description here. You number is higher than max safe integer in js

– deathangel908
Jan 3 at 9:09





Check out enter link description here. You number is higher than max safe integer in js

– deathangel908
Jan 3 at 9:09













Possible duplicate of What is JavaScript's highest integer value that a number can go to without losing precision?

– Peter B
Jan 3 at 9:12





Possible duplicate of What is JavaScript's highest integer value that a number can go to without losing precision?

– Peter B
Jan 3 at 9:12












3 Answers
3






active

oldest

votes


















3














The biggest integer value javacript can hold is +/- 9007199254740991. Note that the bitwise operators and shift operators operate on 32-bit ints, so in that case, the max safe integer is 2^31-1, or 2147483647.



In my opinion, you can choose one of the following:




  • store the numbers as strings and manipulate them as numbers; you might have to implement special functions to add/subtract/multiply/divide them (these are classic algorithmic problems)


  • use the BigInt; BigInts are a new numeric primitive in JavaScript that can represent integers with arbitrary precision. With BigInts, you can safely store and operate on large integers even beyond the safe integer limit. Unfortunately, they work only with Chrome right now. If you want to work with other browsers, you might check this or even this if you work with angularjs or nodejs.



Try the following code in the Chrome's console:



let x = BigInt([6,1,4,5,3,9,0,1,9,5,1,8,6,7,0,5,5,4,3].join(''));
console.log(x);


This will print 6145390195186705543n. The n suffix marks that it is a big integer.



Cheers!






share|improve this answer
























  • Thank you. Actually I am doing a programming exercise called "Add one" and the tests environment does not allow me to use BigInt.

    – user545871
    Jan 3 at 9:41











  • @user545871 You are welcome! You may mark my answer as accepted if it was helpful :) Cheers!

    – Adrian Pop
    Jan 3 at 10:18



















2














You can use JavaScript Array join() Method and parse it into integer.
Example:
parseInt([6,1,4,5,3,9,0,1,9,5,1,8,6,7,0,5].join(''))
results:
6145390195186705



Edited: Use BigInt instead of parseInt , but it works only on chrome browser.






share|improve this answer

































    1














    The largest number possible in Javascript is
    +/- 9007199254740991



    Use BigInt. Join all numbers as a string and pass it in BigInt global function to convert it into int



    var integer = 0;
    var digits = [1,2,3,4]
    //combine array of digits into int
    digits.forEach((num,index,self) => {
    integer += num;
    });
    integer= BigInt(integer);


    Note : Works only on Chrome as of now. You can use othee libraries like BigInteger.js or MathJS






    share|improve this answer


























      Your Answer






      StackExchange.ifUsing("editor", function () {
      StackExchange.using("externalEditor", function () {
      StackExchange.using("snippets", function () {
      StackExchange.snippets.init();
      });
      });
      }, "code-snippets");

      StackExchange.ready(function() {
      var channelOptions = {
      tags: "".split(" "),
      id: "1"
      };
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function() {
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled) {
      StackExchange.using("snippets", function() {
      createEditor();
      });
      }
      else {
      createEditor();
      }
      });

      function createEditor() {
      StackExchange.prepareEditor({
      heartbeatType: 'answer',
      autoActivateHeartbeat: false,
      convertImagesToLinks: true,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: 10,
      bindNavPrevention: true,
      postfix: "",
      imageUploader: {
      brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
      contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
      allowUrls: true
      },
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      });


      }
      });














      draft saved

      draft discarded


















      StackExchange.ready(
      function () {
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54019143%2fissue-with-combining-large-array-of-numbers-into-one-single-number%23new-answer', 'question_page');
      }
      );

      Post as a guest















      Required, but never shown

























      3 Answers
      3






      active

      oldest

      votes








      3 Answers
      3






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      3














      The biggest integer value javacript can hold is +/- 9007199254740991. Note that the bitwise operators and shift operators operate on 32-bit ints, so in that case, the max safe integer is 2^31-1, or 2147483647.



      In my opinion, you can choose one of the following:




      • store the numbers as strings and manipulate them as numbers; you might have to implement special functions to add/subtract/multiply/divide them (these are classic algorithmic problems)


      • use the BigInt; BigInts are a new numeric primitive in JavaScript that can represent integers with arbitrary precision. With BigInts, you can safely store and operate on large integers even beyond the safe integer limit. Unfortunately, they work only with Chrome right now. If you want to work with other browsers, you might check this or even this if you work with angularjs or nodejs.



      Try the following code in the Chrome's console:



      let x = BigInt([6,1,4,5,3,9,0,1,9,5,1,8,6,7,0,5,5,4,3].join(''));
      console.log(x);


      This will print 6145390195186705543n. The n suffix marks that it is a big integer.



      Cheers!






      share|improve this answer
























      • Thank you. Actually I am doing a programming exercise called "Add one" and the tests environment does not allow me to use BigInt.

        – user545871
        Jan 3 at 9:41











      • @user545871 You are welcome! You may mark my answer as accepted if it was helpful :) Cheers!

        – Adrian Pop
        Jan 3 at 10:18
















      3














      The biggest integer value javacript can hold is +/- 9007199254740991. Note that the bitwise operators and shift operators operate on 32-bit ints, so in that case, the max safe integer is 2^31-1, or 2147483647.



      In my opinion, you can choose one of the following:




      • store the numbers as strings and manipulate them as numbers; you might have to implement special functions to add/subtract/multiply/divide them (these are classic algorithmic problems)


      • use the BigInt; BigInts are a new numeric primitive in JavaScript that can represent integers with arbitrary precision. With BigInts, you can safely store and operate on large integers even beyond the safe integer limit. Unfortunately, they work only with Chrome right now. If you want to work with other browsers, you might check this or even this if you work with angularjs or nodejs.



      Try the following code in the Chrome's console:



      let x = BigInt([6,1,4,5,3,9,0,1,9,5,1,8,6,7,0,5,5,4,3].join(''));
      console.log(x);


      This will print 6145390195186705543n. The n suffix marks that it is a big integer.



      Cheers!






      share|improve this answer
























      • Thank you. Actually I am doing a programming exercise called "Add one" and the tests environment does not allow me to use BigInt.

        – user545871
        Jan 3 at 9:41











      • @user545871 You are welcome! You may mark my answer as accepted if it was helpful :) Cheers!

        – Adrian Pop
        Jan 3 at 10:18














      3












      3








      3







      The biggest integer value javacript can hold is +/- 9007199254740991. Note that the bitwise operators and shift operators operate on 32-bit ints, so in that case, the max safe integer is 2^31-1, or 2147483647.



      In my opinion, you can choose one of the following:




      • store the numbers as strings and manipulate them as numbers; you might have to implement special functions to add/subtract/multiply/divide them (these are classic algorithmic problems)


      • use the BigInt; BigInts are a new numeric primitive in JavaScript that can represent integers with arbitrary precision. With BigInts, you can safely store and operate on large integers even beyond the safe integer limit. Unfortunately, they work only with Chrome right now. If you want to work with other browsers, you might check this or even this if you work with angularjs or nodejs.



      Try the following code in the Chrome's console:



      let x = BigInt([6,1,4,5,3,9,0,1,9,5,1,8,6,7,0,5,5,4,3].join(''));
      console.log(x);


      This will print 6145390195186705543n. The n suffix marks that it is a big integer.



      Cheers!






      share|improve this answer













      The biggest integer value javacript can hold is +/- 9007199254740991. Note that the bitwise operators and shift operators operate on 32-bit ints, so in that case, the max safe integer is 2^31-1, or 2147483647.



      In my opinion, you can choose one of the following:




      • store the numbers as strings and manipulate them as numbers; you might have to implement special functions to add/subtract/multiply/divide them (these are classic algorithmic problems)


      • use the BigInt; BigInts are a new numeric primitive in JavaScript that can represent integers with arbitrary precision. With BigInts, you can safely store and operate on large integers even beyond the safe integer limit. Unfortunately, they work only with Chrome right now. If you want to work with other browsers, you might check this or even this if you work with angularjs or nodejs.



      Try the following code in the Chrome's console:



      let x = BigInt([6,1,4,5,3,9,0,1,9,5,1,8,6,7,0,5,5,4,3].join(''));
      console.log(x);


      This will print 6145390195186705543n. The n suffix marks that it is a big integer.



      Cheers!







      share|improve this answer












      share|improve this answer



      share|improve this answer










      answered Jan 3 at 9:22









      Adrian PopAdrian Pop

      1,16741323




      1,16741323













      • Thank you. Actually I am doing a programming exercise called "Add one" and the tests environment does not allow me to use BigInt.

        – user545871
        Jan 3 at 9:41











      • @user545871 You are welcome! You may mark my answer as accepted if it was helpful :) Cheers!

        – Adrian Pop
        Jan 3 at 10:18



















      • Thank you. Actually I am doing a programming exercise called "Add one" and the tests environment does not allow me to use BigInt.

        – user545871
        Jan 3 at 9:41











      • @user545871 You are welcome! You may mark my answer as accepted if it was helpful :) Cheers!

        – Adrian Pop
        Jan 3 at 10:18

















      Thank you. Actually I am doing a programming exercise called "Add one" and the tests environment does not allow me to use BigInt.

      – user545871
      Jan 3 at 9:41





      Thank you. Actually I am doing a programming exercise called "Add one" and the tests environment does not allow me to use BigInt.

      – user545871
      Jan 3 at 9:41













      @user545871 You are welcome! You may mark my answer as accepted if it was helpful :) Cheers!

      – Adrian Pop
      Jan 3 at 10:18





      @user545871 You are welcome! You may mark my answer as accepted if it was helpful :) Cheers!

      – Adrian Pop
      Jan 3 at 10:18













      2














      You can use JavaScript Array join() Method and parse it into integer.
      Example:
      parseInt([6,1,4,5,3,9,0,1,9,5,1,8,6,7,0,5].join(''))
      results:
      6145390195186705



      Edited: Use BigInt instead of parseInt , but it works only on chrome browser.






      share|improve this answer






























        2














        You can use JavaScript Array join() Method and parse it into integer.
        Example:
        parseInt([6,1,4,5,3,9,0,1,9,5,1,8,6,7,0,5].join(''))
        results:
        6145390195186705



        Edited: Use BigInt instead of parseInt , but it works only on chrome browser.






        share|improve this answer




























          2












          2








          2







          You can use JavaScript Array join() Method and parse it into integer.
          Example:
          parseInt([6,1,4,5,3,9,0,1,9,5,1,8,6,7,0,5].join(''))
          results:
          6145390195186705



          Edited: Use BigInt instead of parseInt , but it works only on chrome browser.






          share|improve this answer















          You can use JavaScript Array join() Method and parse it into integer.
          Example:
          parseInt([6,1,4,5,3,9,0,1,9,5,1,8,6,7,0,5].join(''))
          results:
          6145390195186705



          Edited: Use BigInt instead of parseInt , but it works only on chrome browser.







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Jan 3 at 9:31

























          answered Jan 3 at 9:11









          sumit gautamsumit gautam

          398




          398























              1














              The largest number possible in Javascript is
              +/- 9007199254740991



              Use BigInt. Join all numbers as a string and pass it in BigInt global function to convert it into int



              var integer = 0;
              var digits = [1,2,3,4]
              //combine array of digits into int
              digits.forEach((num,index,self) => {
              integer += num;
              });
              integer= BigInt(integer);


              Note : Works only on Chrome as of now. You can use othee libraries like BigInteger.js or MathJS






              share|improve this answer






























                1














                The largest number possible in Javascript is
                +/- 9007199254740991



                Use BigInt. Join all numbers as a string and pass it in BigInt global function to convert it into int



                var integer = 0;
                var digits = [1,2,3,4]
                //combine array of digits into int
                digits.forEach((num,index,self) => {
                integer += num;
                });
                integer= BigInt(integer);


                Note : Works only on Chrome as of now. You can use othee libraries like BigInteger.js or MathJS






                share|improve this answer




























                  1












                  1








                  1







                  The largest number possible in Javascript is
                  +/- 9007199254740991



                  Use BigInt. Join all numbers as a string and pass it in BigInt global function to convert it into int



                  var integer = 0;
                  var digits = [1,2,3,4]
                  //combine array of digits into int
                  digits.forEach((num,index,self) => {
                  integer += num;
                  });
                  integer= BigInt(integer);


                  Note : Works only on Chrome as of now. You can use othee libraries like BigInteger.js or MathJS






                  share|improve this answer















                  The largest number possible in Javascript is
                  +/- 9007199254740991



                  Use BigInt. Join all numbers as a string and pass it in BigInt global function to convert it into int



                  var integer = 0;
                  var digits = [1,2,3,4]
                  //combine array of digits into int
                  digits.forEach((num,index,self) => {
                  integer += num;
                  });
                  integer= BigInt(integer);


                  Note : Works only on Chrome as of now. You can use othee libraries like BigInteger.js or MathJS







                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  edited Jan 3 at 9:21

























                  answered Jan 3 at 9:09









                  Sandesh GuptaSandesh Gupta

                  9092815




                  9092815






























                      draft saved

                      draft discarded




















































                      Thanks for contributing an answer to Stack Overflow!


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid



                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.


                      To learn more, see our tips on writing great answers.




                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function () {
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54019143%2fissue-with-combining-large-array-of-numbers-into-one-single-number%23new-answer', 'question_page');
                      }
                      );

                      Post as a guest















                      Required, but never shown





















































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown

































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown







                      Popular posts from this blog

                      MongoDB - Not Authorized To Execute Command

                      in spring boot 2.1 many test slices are not allowed anymore due to multiple @BootstrapWith

                      How to fix TextFormField cause rebuild widget in Flutter