-2

I've been trying to add the following numbers using javascript; 76561197960265728 + 912447736

Sadly, because of rounding in javascript it will not get the correct number, I need the number as a string.

I've tried removing the last few digits using substr and then adding the two numbers together and then putting the two strings together, sadly this doesn't work if the first of the number is a 1.

function steamid(tradelink){
    var numbers = parseInt(tradelink.split('?partner=')[1].split('&')[0]),
            base = '76561197960265728';

    var number = parseInt(base.substr(-(numbers.toString().length + 1))) + numbers;
            steamid = //put back together
}

steamid('https://steamcommunity.com/tradeoffer/new/?partner=912447736&token=qJD0Oui2');

Expected: 76561198872713464

1 Answers1

4

welcome to StackOverflow.

For doing operations with such big integers, you should use BigInt, it will correctly operate on integers bigger than 2ˆ53 (which is the largest size that normal Number can support on JS

const sum = BigInt(76561197960265728) + BigInt(912447736);

console.log(sum.toString());

Edit 2020: This API still not widely supported by some browsers, so you may need to have a polyfill, please check this library

Renan Souza
  • 815
  • 7
  • 24