0

Can anyone explain why this unexpected results?

parseInt(999999999999999999999) // equals 1 parseInt(9999999999999999) // equals 10000000000000000

23rd Apr 2021, 7:00 PM
Apongpoh Gilbert
Apongpoh Gilbert - avatar
6 Respuestas
+ 6
Why are you parsing an integer to an integer? Maybe the reason why the first one is giving us 1 is because I think it's exceeds the Number.MAX_SAFE_INTEGER (constant representing the maximum safe integer in JavaScript (253 - 1)) and therefore leading us to this weird result of 1. You may use BigInt for very large value. console.log(Number.MAX_SAFE_INTEGER) console.log(parseInt(999999999999999999999)) // equals 1 console.log(BigInt("999999999999999999999"))
23rd Apr 2021, 7:16 PM
RKK
RKK - avatar
+ 2
Rae asks a good question: why are you passing in an integer to parseInt? It should be a string. What's happening in your code is that the integer arguments are first being converted using toString() before being parsed into integers by parseInt(). Both your numbers are bigger than the max safe integer. So they lose precision which is why you should use bigints. The string representation of your bigger number is '1e+21'. When this is passed to parseInt(), the character 'e' means that the 'e' and everything after that is cut off. That's why you end up with 1. For the other number, although it isn't big enough to need the above notation (with 'e'), it loses precision for the reason I stated above. So the incorrect, imprecise number is converted to a string which is what you see parsed to an integer beginning with 1 followed by lots of zeroes.
23rd Apr 2021, 9:45 PM
CamelBeatsSnake
CamelBeatsSnake - avatar
0
rkk and CamelBeatsSnake from definition -> The parseInt function converts its first argument to a string, parses that string, then returns an integer or NaN. Now my code -> passing an integer as first argument does not produce any errors(eg syntaxError etc...). Why not asked yourself and consider the first argument is from a user input or from some computation.
23rd Apr 2021, 10:22 PM
Apongpoh Gilbert
Apongpoh Gilbert - avatar
0
Apongpoh Gilbert Just because there is no error thrown doesn't mean the function will automatically do what you expect. Remember that JavaScript is a dynamically typed language. You cannot pass an integer that big into parseInt() and expect it to convert to the string representation you seem to want. The string conversion of those big numbers means parseInt gives you different results than you seem to want. You will see that if you read further into the docs. It doesn't matter if the value comes from a user or not. Clearly with this function and the sizes of values you have as the developer, you will need to do something else rather than simply passing it to parseInt(). That's part of the role of a developer. To get your data to the right structure and type before passing it into things.
23rd Apr 2021, 10:51 PM
CamelBeatsSnake
CamelBeatsSnake - avatar
0
CamelBeatsSnake Don't get me wrong bro you going off topic from your last comment. Read my question again. Try answer it, if you can't then ignore.
23rd Apr 2021, 11:02 PM
Apongpoh Gilbert
Apongpoh Gilbert - avatar
0
Apongpoh Gilbert No worries. Perhaps you have misunderstood but I answered your question in my first response. I'm if not mistaken, your question was "Can anyone explain why this unexpected results?". I explained it. Please re-read my first answer if you need to. Did you have another question?
23rd Apr 2021, 11:14 PM
CamelBeatsSnake
CamelBeatsSnake - avatar