bignumber.js
2022-07-19 本文已影响0人
小溪流jun
bignumber.js
用于任意精度十进制和非十进制算术的 JavaScript 库。
gitHub官网地址:https://github.com/MikeMcl/bignumber.js
安装
npm install --save bignumber.js
plus():加法
import BigNumber from "bignumber.js";
export default {
mounted() {
let x = new BigNumber(0.1);
let y = x.plus(0.2);
console.log("精度丢失", 0.1 + 0.2); //0.30000000000000004
console.log("y", y.toNumber()); //0.3
},
};
minus():减法
import BigNumber from "bignumber.js";
export default {
mounted() {
let x = new BigNumber(0.4);
let y = x.minus(0.1);
console.log("精度丢失", 0.4 - 0.1); //0.30000000000000004
console.log("y", y.toNumber()); //0.3
},
};