1

Use

Hi, so I was working on my discord bot with some user ids that I stored in a database, which then I will get back to ping them or give them roles.

Problem

Though here's the problem, in the database they get turned from ex: 533692905387196429 to 533692905387196400. I tried setting that to a String, and it worked, in the database, it's stored fully how it's supposed to be. But, when I get them back from the database and turn them into a number or an integer they get turned back to ex: 533692905387196400.

Tried using

I tried using parseInt(), parseFloat() and Number() but all of them give the same result.

More info

Also if I remove 2 digits for example: 533692905387196429I remove 19 from there, it will give back 533692905387(19)6429 instead of 533692905387196429 and instead of 533692905387196400.

Any help is appreciated!

  • Could you please add some code to your question? We need to see where is the issue being generated: when you get info from DB or when computing it, etc. – Juan Medina Apr 07 '21 at 22:06
  • You could check integers for overflow using `Number.isSafeInteger` to safe guard against such issues. – Ravi Tiwari Apr 07 '21 at 22:08
  • Does this answer your question? [What is JavaScript's highest integer value that a number can go to without losing precision?](https://stackoverflow.com/questions/307179/what-is-javascripts-highest-integer-value-that-a-number-can-go-to-without-losin) – jonny Apr 07 '21 at 22:13

2 Answers2

1

So, Number.MAX_SAFE_INTEGER is 9007199254740991 (which is 253 - 1). Your number is too large to hold full precision.

If you want a number with that level of digits and precision, you can use BigInt and then you will have to be very careful how you use that BigInt as it cannot be directly used in place of a regular Number type, but it can hold infinite precision and you can do math between two BigInt values.

Whether it's best used as a BigInt or as a String really depends upon what you're doing with it. If these are just some sort of ID that you aren't actually doing numeric operations with, then you can just keep it as a string.

jfriend00
  • 683,504
  • 96
  • 985
  • 979
  • +1. I would add to this that string is 9 times out of 10 the way to go if you want arbitrarily large identifiers. Strings are far easier to reason about than special types like BigInt, which are really overkill imo – jonny Apr 07 '21 at 22:10
0

The number is too big, so Javascript doesn't keep full precision!

> 533692905387196429
533692905387196400

You can resolve this by storing them as strings:

> '533692905387196429'
'533692905387196429'

You shouldn't need to do any mathematical operations with Discord IDs so there shouldn't be any issue storing and treating them as strings everywhere.

DemiPixel
  • 1,768
  • 11
  • 19
  • Read my topic again. And also I need them to be numbers or integers when I use them, not strings. If you didn't read fully i already said I tried storing them as strings it worked fine but when I get them back they get corrupted at the end again.. –  Apr 07 '21 at 22:05
  • @VikkiVuk well unfortunately that's not possible in javascript, unless you convert to a special type like [BigInt](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/BigInt) which can store more bits than the the `number` type – jonny Apr 07 '21 at 22:06
  • Discord IDs or "Snowflakes" are stored as strings in Discord.js, so there's really no reason you should need to convert them to an integer: https://discord.js.org/#/docs/main/stable/typedef/Snowflake – DemiPixel Apr 07 '21 at 22:10