0

I want to convert an integer to number with specific base. In this case it's 36.

So as documentation states I do myNumber.toString(36).

Here are the results:

console.log((12345678901234566).toString(36));
console.log((12345678901234567).toString(36));
console.log((12345678901234568).toString(36));
console.log((12345678901234569).toString(36));
console.log((12345678901234570).toString(36));
console.log((12345678901234571).toString(36));

as you can see there's something off because the output is not unique.

From what I've found Number.MAX_SAFE_INTEGER = 9007199254740991 and my numbers exceed that value so I believe that's why it's treated differently

It's a shame that it's not stated anywhere in the documentation that there are some boundaries. Maybe it's common knowledge, I don't know.

Any ideas how to approach this issue for bigger numbers?

I can see that here it's implemented wrongly (as in my example) and here correctly but I don't know the logic behind it.

Kamil
  • 1,456
  • 4
  • 32
  • 50
  • 1
    `Maybe it's common knowledge` Most native types of any programming language have some form of limits,.. But what you can do is use a third party lib to handle big number,.. There are lots of them out there, here is one I just googled. https://mikemcl.github.io/bignumber.js/ – Keith Jul 05 '18 at 10:51
  • 1
    Related: https://stackoverflow.com/questions/588004/is-floating-point-math-broken – Jonas Wilms Jul 05 '18 at 10:53
  • See [Large numbers erroneously rounded in Javascript](https://stackoverflow.com/q/1379934/4642212). – Sebastian Simon Jul 05 '18 at 10:56
  • Those numbers would surprise you even if you logged them in base 10 ;) Read on MDN about [safe integers](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/isSafeInteger). – barbsan Jul 05 '18 at 11:03

0 Answers0