Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

224
Views
How to convert a long string (more than 16 digits) into numbers

this is a function just increment one number into array but the problem i interface when i put alot of numbers into array (more than 16 digits) when i use parseInt() just returned 16 correct numbers and more than that be zero

6145390195186705000

and expected

6145390195186705543

the function

var plusOne = function(digits) {
    var numbersInString = digits.join('');
    var theNumbers = parseInt(numbersInString);
    var theNumbersPlusOne = theNumbers + 1;
    var result = String(theNumbersPlusOne).split("").map((theNumbersPlusOne) => {
        return Number(theNumbersPlusOne);
    });
    return result;
};

console.log(plusOne([6,1,4,5,3,9,0,1,9,5,1,8,6,7,0,5,5,4,3]));

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

You can use BigInt to handle this problem.

var plusOne = function(digits) {
    var numbersInString = digits.join('');
    var theNumbers = BigInt(numbersInString);
    var theNumbersPlusOne = theNumbers + BigInt(1);
    var result = theNumbersPlusOne.toString().split("").map((theNumbersPlusOne) => {
        return Number(theNumbersPlusOne);
    });
    return result;
};
console.log(plusOne([6,1,4,5,3,9,0,1,9,5,1,8,6,7,0,5,5,4,3]));

about 4 years ago · Juan Pablo Isaza Report

0

Just expanding on my above comment with another solution...

You've exceeded the maximum safe integer value. (Number.MAX_SAFE_INTEGER, which equals 9007199254740991). Numbers larger than this are not supported with standard integer types in javascript, or rather there's not enough precision to represent them. Anything larger than this is represented in scientific notation and the extra digits are truncated and represented only as zeroes.

With that said, you don't even need to convert the array to a string to an integer just to increment it. You can just increment the individual digits in the array, starting at the end and working your way forwards to "carry the 1" so to speak.

var plusOne = function(digits) {
    for(let i = digits.length - 1; i > -1; i--)
    {
      if(digits[i] == 9)
      {
        digits[i] = 0;
        if(i == 0)
          digits = [1].concat(digits);
      }
      else
      {
        digits[i]++;
        break;
      }
    }
    return digits;
};

console.log(plusOne([6,1,4,5,3,9,0,1,9,5,1,8,6,7,0,5,5,4,3]));

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!