ignumber.js用于数字精度要求较高的计算。
bignumber.js源码地址:https://github.com/MikeMcl/bignumber.js
1.下载bignumber.js。
下载地址:https://github.com/MikeMcl/bignumber.js/releases
2.引入js。
3.示例
html:
<div><label>数1:</label><input id="n1" type="text"><br><label>数2:</label><input id="n2" type="text"><br><button id="add0">加(原生)</button><button id="add">加(BigNumber)</button><br><label>结果: </label><span id="res"></span></div>
js:
$(function(){$("#add0").click(function(){var n1Text=$("#n1").val();var n2Text=$("#n2").val();var _n1=parseFloat(n1Text);var _n2=parseFloat(n2Text);var _res=_n1+_n2;$("#res").text(_res);});$("#add").click(function(){var n1Text=$("#n1").val();var n2Text=$("#n2").val();var n1=new BigNumber(n1Text);var n2=new BigNumber(n2Text);var res=n1.plus(n2);$("#res").text(res);}); })
4、bignumber.js API地址:
http://mikemcl.github.io/bignumber.js/
参考文章:https://www.jianshu.com/p/bbb3fbbe9299
https://www.css88.com/archives/9318
其他js高精度小数运算库(未亲自尝试):
decimal.js:https://www.jianshu.com/p/3ecbe7584043
BigDecimal:http://www.cnblogs.com/liuliu2017/p/7986341.html