最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Javascript: toString(36) for large integers - Stack Overflow

programmeradmin1浏览0评论

15955067621307336078.toString(36); returns '3d7vzfy5k2as8' in Javascript because the large integer cannot be represented (the correct answer is '3d7vzfy5k29ou').

Does someone have a clever function that takes a large integer as a string and converts it to base 36?

15955067621307336078.toString(36); returns '3d7vzfy5k2as8' in Javascript because the large integer cannot be represented (the correct answer is '3d7vzfy5k29ou').

Does someone have a clever function that takes a large integer as a string and converts it to base 36?

Share Improve this question asked Nov 26, 2012 at 21:38 MeekohiMeekohi 10.9k6 gold badges51 silver badges59 bronze badges 4
  • 2 @Cameron: en.wikipedia.org/wiki/Base_36 – Robert Harvey Commented Nov 26, 2012 at 21:44
  • Actually, I get "3d7vzfy5k2a68" for 15955067621307336078..toString(36) – Bergi Commented Nov 26, 2012 at 21:53
  • I tried writing my own base conversion function, but it stops working completely after a certain size. jsfiddle.net/SEZXq/1 – HellaMad Commented Nov 26, 2012 at 22:08
  • @Bergi - The largest int that JavaScript supports is 9007199254740992. You're number is over that, so the answer you get is determined by how the implementation misbehave when there is overflow. See this thread. – Ted Hopp Commented Nov 26, 2012 at 22:21
Add a comment  | 

2 Answers 2

Reset to default 9

Use this BigInt class, which allows conversion of arbitrarily large integers to any arbitrary base between 2 and 95. Use the bigInt2str() method to perform the conversion.

For anyone curious, today we are using https://github.com/MikeMcl/bignumber.js instead.

发布评论

评论列表(0)

  1. 暂无评论