1

I'm using node.js + express for my project. I end up with big string like "365958975201738764" which needs to be converted into an int.

So I need to take "365958975201738764" which is a string and make it 365958975201738764 which is an int.

Problem is, whenever I do the conversions, js messes up.

I did some research and found out that 9007199254740991 is the biggest number that js can work with, so is there a workaround to this?

Anish Anne
  • 193
  • 3
  • 15
  • 2
    Possible duplicate of [What is the standard solution in JavaScript for handling big numbers (BigNum)?](https://stackoverflow.com/questions/3072307/what-is-the-standard-solution-in-javascript-for-handling-big-numbers-bignum) and https://stackoverflow.com/questions/4288821/how-to-deal-with-big-numbers-in-javascript – ggorlen Aug 13 '19 at 17:26
  • JavaScript often uses IEEE754 (64 bits) for numbers. See http://floating-point-gui.de/ – Basile Starynkevitch Aug 13 '19 at 17:26
  • I can't store a string as a decimal...it's already a string. – Anish Anne Aug 13 '19 at 17:41
  • Other solutions discussed on stackoverflow suggest using a library. BigInt is a safer solution in a long run as it is part of the language. – Saeed D. Aug 13 '19 at 18:03
  • I'm very confused. I have a number bigger than max number as an int, how do I get it into number form? – Anish Anne Aug 13 '19 at 18:14
  • @Anish Anne It depends on what you plan to do with that number. Are you storing it somewhere or do you have to perform some calculations? – Saeed D. Aug 13 '19 at 18:36
  • Please explain 'js messes up'. What is the unexpected result compare to what you expect to see. – Saeed D. Aug 13 '19 at 18:39
  • Aight so I get a string, `"365958975201738764"`. I do `var val=parseInt("365958975201738764",10)`. Then I do `console.log(val)` and get `365958975201738750`. I use the number to fetch a user. `await discordclient.fetchUser(the id)` – Anish Anne Aug 13 '19 at 18:51
  • @AnishAnne `discordClient.fetchUser` expects a string anyway. This seems like an X/Y problem – Paul Aug 13 '19 at 19:10
  • yea I realized that after – Anish Anne Aug 17 '19 at 19:09

1 Answers1

0

Numbers in JavaScript are represented as double-precision floats. This means they have limited precision. The Number.MAX_SAFE_INTEGER constant gives the greatest possible integer that can safely be incremented. 2^53=9,007,199,254,740,992

BigInts are a new numeric primitive in JavaScript that can represent integers with arbitrary precision. With BigInts, you can safely store and operate on large integers even beyond the safe integer limit for Numbers.

To create a BigInt, add the 'n' suffix to any integer literal. For example, 789 is Int and 789n is BigInt. The global BigInt(number) function can be used to convert a Number into a BigInt.

> BigInt(Number.MAX_SAFE_INTEGER)
// returns 9007199254740991n

For example, you can calculate MAX_SAFE_INTEGER to the power of two

> 9007199254740991n ** 2n
// returns 81129638414606663681390495662081n

For more detailed information and examples arbitrary-precision integers in JavaScript and BigInt - JavaScript | MDN

Saeed D.
  • 1,125
  • 1
  • 11
  • 23
  • So i have `"365958975201738764"` as a string, how do I make it an int? – Anish Anne Aug 13 '19 at 18:12
  • so instead of `Number("365958975201738764")` its `BigInt("365958975201738764")` ? – Anish Anne Aug 13 '19 at 18:17
  • I'm getting the error `ReferenceError: BigInt is not defined` How do I define it? – Anish Anne Aug 13 '19 at 18:35
  • 1
    you can use node v10.4 onward for bigint support – Nayan Patel Aug 15 '19 at 18:17
  • Keep in mind `BigInt` is not fast. It's very high level and must do a lot of internal conversions under the hood. Something to keep in mind if you're doing heavy workloads, simulations, etc. However, 64-bit integers are available in WebAssembly. – bryc Apr 24 '21 at 19:38