美文网首页
mathjs用法

mathjs用法

作者: 放任自由f0 | 来源:发表于2021-10-14 13:32 被阅读0次

    import * as math from "mathjs"
    0.1+0.2
    math.format(math.chain(math.bignumber(0.1)).add(math.bignumber(0.2)).done());

    0.2-0.1
    math.format(math.chain(math.bignumber(0.2)).subtract(math.bignumber(0.1)).done());

    0.1*0.2
    math.format(math.chain(math.bignumber(0.1)).multiply(math.bignumber(0.2)).done());

    0.1/0.2
    math.format(math.chain(math.bignumber(0.1)).divide(math.bignumber(0.2)).done());

    "mathjs": "^10.6.4",
    import { create, all } from 'mathjs'
    const math = create(all, { number: 'BigNumber', precision: 20 })
    const { format, bignumber, add, subtract, multiply, divide } = math;
    // 计算
    export function calculation(type, ...rest) {
    // add加 subtract减 multiply乘 devide除
    let opear = add;
    switch (type) {
    case '+': // 加
    opear = add;
    break;
    case '-': // 减
    opear = subtract;
    break;
    case '*': // 乘
    opear = multiply;
    break;
    case '/': // 除
    opear = divide;
    break;
    }
    return rest.reduce((total, item) => +format(opear(bignumber(total), bignumber(item))));
    }

    使用
    calculation('+',0.1,0.2)

    相关文章

      网友评论

          本文标题:mathjs用法

          本文链接:https://www.haomeiwen.com/subject/gjtuoltx.html