import { create, all, MathJsStatic, format } from "mathjs";
/**
* @description: 精确计算
* @author ChenRui
* @date 2021/6/9 9:44
*/
export class MathService {
static DEFAULT_PRECISION = 20; // 默认保留的小数位数
math: Partial<MathJsStatic> | any = create(all, { number: "BigNumber", precision: MathService.DEFAULT_PRECISION });
/**
* @description: 计算函数
* @param: formula: 计算公式; precision: 保留的小数位数
* @author ChenRui
* @date 2021/6/9 9:42
*/
evaluate(formula: string, precision: number = MathService.DEFAULT_PRECISION): void {
const res: any = format(this.math.evaluate(formula), { notation: "fixed", precision: precision });
return res;
}
}
const mathService: MathService = new MathService();
export default mathService;
-
由 jiangzaicheng 提交于b6851288