-2

this is code

const str = '1111';
console.log(Number(str));

But When String's length over 17, it will be have problem

const str = '111100000123121221234'
console.log(Number(str)); // 111100000123121220000

I want to know, what's the principle of convert string to Integer of Number Object. Why convert wrong

Thank You Very Much

Eddie
  • 26,593
  • 6
  • 36
  • 58
SecretCastle
  • 5,299
  • 3
  • 9
  • 7
  • 1
    You exceeded the maximum precision for integers in JS. That's why the number comes back "wrong". – Sirko May 30 '18 at 13:20
  • Because that number is greater than MAX_SAFE_INTEGER(https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/MAX_SAFE_INTEGER) – Kavindra May 30 '18 at 13:22

1 Answers1

1

Because "Integers (numbers without a period or exponent notation) are accurate up to 15 digits". See: https://www.w3schools.com/js/js_numbers.asp

Mikhail Zakharov
  • 904
  • 11
  • 22