文档地址:https://mikemcl.github.io/bignumber.js/
npm install bignumber.js --S
import { BigNumber } from 'bignumber.js';
加法
add(a, b) { a = BigNumber(a); b = BigNumber(b); return a.plus(b).toNumber(); //结果需要用toNumber转为普通数字 } //调用 add(0.1,0.2) //0.3
减
minus(a, b) { a = BigNumber(a); b = BigNumber(b); return a.minus(b).toNumber(); }, minus(0.3,0.1); //0.2
乘
mutiply(a, b) { a = BigNumber(a); b = BigNumber(b); return a.multipliedBy(b).toNumber(); } mutiply(3,0.6); //1.8
除法
devide(a, b) { a = BigNumber(a); b = BigNumber(b); return a.dividedBy(b).toNumber(); } devide(355,113) // 3.1415929203539825
多个数求和
multiAdd(...params) { let data = BigNumber(0); for (let index = 0; index < params.length; index++) { const element = BigNumber(params[index]); data = data.plus(element); } return data.toNumber(); } multiAdd(1,2,3,4,5); //15