import { create, all } from 'mathjs'
// 配置高精度计算(最多20位小数)
const math = create(all, {
number: 'BigNumber',
precision: 20
})
export function calculateWithMathjs(formula) {
try {
const result = math.evaluate(formula) // 使用mathjs库进行计算
return math.format(result, { precision: 10, notation: 'fixed' }) // 返回计算结果
} catch (error) {
console.error('计算错误:', error) // 处理计算错误
return null // 返回null表示计算错误
}
}
calculateWithMathjs('(0.1 + 1) * 1.05 + 0.2 + 0.1')