-1

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

9

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

Edit 2023 This API is now supported on almost everything but internet explorer, so you can probably safely use this without polyfills unless you have strict legacy browser support requirements.

Renan Souza
  • 905
  • 9
  • 25