I write a code in javascript and I need to convert a string to an integer, but I understood the parseInt and parseFloat, but do not convert the string correctly.
let stringNum = "9875987598759875";
let parsedString = parseInt(stringNum);
console.log(stringNum); //print 9875987598759875
console.log(parsedString); //print 9875987598759876
I can't understand why my parsedString is different. I test parseFloat too but I have the same problem.
I need this value as an integer for some calculations.
9875987598759875 exceeds JavaScript's MAX_SAFE_INTEGER constant of 2^53 - 1. Use BigInt if you have a reasonable expectation that you'll be consistently dealing with numbers in this order of magnitude:
let stringNum = "9875987598759875";
let parsed = BigInt(stringNum);
console.log(parsed.toString());