javascript - JS中大数算术运算如何得到精确的整数结果?

标签 javascript

JS中大数的除运算如何得到精确的整数结果?

假设我们有:

var a = 75643564363473453456342378564387956906736546456235342;
var b = 34986098309687982743378379458582778387652482;

将这些数字相乘得到一个采用科学“e”表示法的数字。如何显示整个精确整数结果数?

我尝试过使用这个库 https://github.com/peterolson/BigInteger.js

自述文件说:

Note that Javascript numbers larger than 9007199254740992 and smaller than -9007199254740992 are not precisely represented numbers and will not produce exact results. If you are dealing with numbers outside that range, it is better to pass in strings.

但是如何用字符串进行数学运算呢?

最佳答案

使用bigInt,正如您所暗示的,您需要使用字符串而不是数字来正确表示它们。

bigInt("75643564363473453456342378564387956906736546456235342").multiply("34986098309687982743378379458582778387652482");

关于javascript - JS中大数算术运算如何得到精确的整数结果?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45352173/

相关文章:

javascript - 如何将 x-www-form-urlencoded 字符串转换为 JSON?

javascript - AngularJS ng-repeat 用于行和项目

javascript - 如何 JSON 字符串化 javascript 日期并保留时区

javascript - Chrome 扩展程序内容脚本中的 polymer 元素

javascript - 阻止输入文本字段中的字符,将输入镜像到 span 或 div

javascript - 在 JavaScript 函数中使用全局变量

javascript - 将 JavaScript 链接到 HTML 文件

javascript - 在某些情况下无法重置属性,vue.js

javascript - 尝试使用 Chart Js 和 React 导出图表但出现错误

javascript - 如何使用 Element.PropertyName IE 浏览器将值设置为 “text”?