0

I have a Laravel application that produces 17 digit (64 bit) numeric IDs (laravel-snowflake)

When this collection is rendered into an Inertia Vue component as a prop, the last 2 digits are being rounded to the nearest 10.

37648503920529408 is being rounded to 37648503920529410

37648503945695232 is being rounded to 37648503945695230

This doesn't happen all the time, but it does for most IDs.

I've dumped the collection through each class of the pipeline and it always matches the database, only when it gets into Vue are the IDs rounded.

Any explanations for this?

5
  • 3
    That’s because your ID is a number representation and not a string, and numeric representation in JS is capped by Number.MAX_SAFE_INTEGER which is 9007199254740991. Your 17 digit number exceeds that range. The solution is to not cast to a number but a string instead. Commented Aug 13, 2020 at 13:15
  • AHA. I knew it had to be something like that. I couldn't find it documented anywhere. So if I change this to 15 or 16 digits I can keep it as a number? Will I see any performance difference casting that to a string? Should I choose a different method to generate the IDs? Commented Aug 13, 2020 at 13:39
  • Is there a way to tell Javascript to cast this to a BigInt? This is a BigInteger in MySQL. Commented Aug 13, 2020 at 13:43
  • developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/… Commented Aug 13, 2020 at 14:10
  • There is no reason to cast it to a number to begin with, since you're never going to use the ID for numerical operations I suppose. Commented Aug 13, 2020 at 14:30

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.