0

I was expecting 6145390195186705543 but gave me 6145390195186705000. This is my code:

var str = '6145390195186705543';
var num = parseInt(str);
console.log(num)
Elikill58
  • 4,050
  • 24
  • 23
  • 45
CodeBaze
  • 1
  • 1

1 Answers1

0

JavaScript uses 64-bit floating point numbers, which can represent numbers in the range -(2^53 - 1) to (2^53 - 1). In this case, the number 6145390195186705543 is out of this range and cannot be accurately represented in JavaScript. When trying to convert the string "6145390195186705543" to a number using the parseInt() function, JavaScript cannot represent the number exactly and rounds it to the nearest floating point number, resulting in 6145390195186705000.

To work with such large numbers, you can use special libraries for working with large arithmetic, such as BigInt or BigNumber.

AlHill
  • 1
  • For large integers there is no need for special libraries. It is natively supported. Moreover, this question has been asked many times before. – trincot Feb 16 '23 at 16:03
  • I tried BigInt yet still it did not work it gave me 6145390195186705543n.Thanks by the way – CodeBaze Feb 17 '23 at 16:16