JavaScript 中处理大数 (BigNum) 的标准解决方案是什么?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3072307/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 03:11:13  来源:igfitidea点击:

What is the standard solution in JavaScript for handling big numbers (BigNum)?

javascriptfloating-accuracybignum

提问by David Cary

Is there a bignum library for JavaScript or built-in that I can include like

是否有一个用于 JavaScript 或内置的 bignum 库,我可以包括

<script type="text/javascript" src="the_bignum_library.js"></script>

?

?

I think my users would prefer to enter numbers in a web page and wait 7 seconds for a result, rather than download an executable and click through a bunch of "this executable could possibly harm your computer" warning screens to install it.

我认为我的用户更愿意在网页中输入数字并等待 7 秒以获得结果,而不是下载可执行文件并单击一堆“此可执行文件可能会损害您的计算机”警告屏幕来安装它。

I've considered basing my own off of http://github.com/silentmatt/javascript-bigintegeror http://www.mainebrook.com/john/fun/euler.html. Or would you recommend calling from JavaScript into a Java bignum library such as apfloat?

我已经考虑过基于http://github.com/silentmatt/javascript-bigintegerhttp://www.mainebrook.com/john/fun/euler.html。或者你会推荐从 JavaScript 调用到 Java bignum 库,比如 apfloat?

采纳答案by jnnnnn

Update(2019-08-19): BigIntis now part of Firefox and Chrome; you no longer need a library:

更新(2019-08-19):BigInt现在是 Firefox 和 Chrome 的一部分;你不再需要图书馆:

const bigInt1 = 1111111111111111111111111111111n;
const bigInt2 = BigInt("1111111111111111111111111111111")
console.log((bigInt1 + bigInt2)+"")

Original answer:

原答案:

If you need arbitrary-precision decimal numbers, use Javascript-bignum, as it is correct and fast.

如果您需要任意精度的十进制数,请使用Javascript-bignum,因为它正确且快速。

回答by Babiker