ParseInt returns values ​​that differ by 1

I have the following data:

var currentValue="12345678901234561";

and I'm trying to parse it:

var number = parseInt(currentValue, 10) || 0;

and my result is:

number = 12345678901234560

now try:

currentValue="12345678901234567"

in this case parseInt(currentValue,10) will result in12345678901234568

Can someone explain to me why parseInt adds / subtracts 1 from the values ​​provided by me?

+4
source share
1 answer

Can someone explain to me why parseInt adds / subtracts 1 from the values ​​provided by me?

This is not the case, but IEEE-754 double-precision binary floating-point JavaScript numbers (even if you are using parseInt) that have only about 15 digits of precision. Your number is 17 digits, so it undergoes accuracy, and the lower digits become spongy.

- 9 007 199,254,740,991, Number.MAX_SAFE_INTEGER JavaScript. (, Number.MIN_SAFE_INTEGER, -9,007,99,254,740,991.)

, ~, & |, 32- , : -2 31 (-2,147,483,648) 2 31 -1 (2 147 483 647). , <<, >> >>>, 32- , 0 4 294 967 295. , , length 32- .

+8

Source: https://habr.com/ru/post/1612355/


All Articles