WishMeLz

生活其实很有趣

decimal.js 加减乘除

import Decimal from 'decimal.js';

// 加法
function add(...nums: number[]): number {
  return nums.reduce((total, num) => new Decimal(total).plus(num), 0).toNumber();
}

// 减法
function subtract(...nums: number[]): number {
  return nums.reduce((total, num) => new Decimal(total).minus(num)).toNumber();
}

// 乘法
function multiply(...nums: number[]): number {
  return nums.reduce((total, num) => new Decimal(total).times(num), 1).toNumber();
}

// 除法
function divide(...nums: number[]): number {
  return nums.reduce((total, num) => new Decimal(total).dividedBy(num)).toNumber();
}