Large numbers to string in JavaScript

此生再无相见时 提交于 2019-12-01 16:43:28

问题


When I do the following:

alert((2053716830872415770228778006271971120334843128349550587141047275840274143041).toString());

I get the "2.053716830872415e+75" exponential notation. I want to alert the number as is. I tried toFixed(), toPrecision() and toLocaleString() and they output the same thing.

How can I accomplish this? Note: the number is a variable, so I cannot alert("208..");


回答1:


Javascript uses 64-bit floating point numbers.
It is impossible to precisely store your value in a Javascript number.

Instead, you should use a BigInteger library.

  • http://www.leemon.com/crypto/BigInt.html
  • http://www-cs-students.stanford.edu/~tjw/jsbn/
  • https://github.com/jtobey/javascript-bignum


来源:https://stackoverflow.com/questions/4985153/large-numbers-to-string-in-javascript

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!