import Decimal from "decimal.js";

/**
 * 加法
 * @param {*} num1 
 * @param {*} num2 
 * @returns 
 */
export const add = (num1, num2) => {
  const flag = (!parseFloat(num1) && parseFloat(num1) != "0") || (!parseFloat(num2) && parseFloat(num2) != "0");
  if (flag) throw new Error("传入参数错误，参数不为number");
  const decimal1 = new Decimal(num1);
  const decimal2 = new Decimal(num2);
  return decimal1.plus(decimal2).toString();
};

/**
 * 减法
 * @param {*} num1 
 * @param {*} num2 
 * @returns 
 */
export const subtract = (num1, num2) => {
  const flag = (!parseFloat(num1) && parseFloat(num1) != "0") || (!parseFloat(num2) && parseFloat(num2) != "0");
  const decimal1 = new Decimal(num1);
  const decimal2 = new Decimal(num2);
  return decimal1.minus(decimal2).toString();
};

/**
 * 乘法
 * @param {*} num1 
 * @param {*} num2 
 * @returns 
 */
export const multiply = (num1, num2) => {
  const flag = (!parseFloat(num1) && parseFloat(num1) != "0") || (!parseFloat(num2) && parseFloat(num2) != "0");
  const decimal1 = new Decimal(num1);
  const decimal2 = new Decimal(num2);
  return decimal1.times(decimal2).toString();
};

/**
 * 除法
 * @param {*} num1 
 * @param {*} num2 
 * @returns 
 */
export const divide = (num1, num2) => {
  const flag = (!parseFloat(num1) && parseFloat(num1) != "0") || (!parseFloat(num2) && parseFloat(num2) != "0");
  const decimal1 = new Decimal(num1);
  const decimal2 = new Decimal(num2);
  return decimal1.dividedBy(decimal2).toString();
};
