/*
 * @Author: zhou lei
 * @Date: 2023-02-22 19:08:18
 * @LastEditTime: 2023-02-22 19:19:54
 * @LastEditors: zhou lei
 * @Description:自定义减法，解决浮点类型相减精度问题
 * @FilePath: \hgwms-factory-app\src\utils\subtraction.ts
 * 联系方式:910592680@qq.com 18669792120
 */
//自定义减法，解决浮点类型相减精度问题
export function subtraction(arg1: number, arg2: number) {
  let r1: number, r2: number;
  try {
    r1 = arg1.toString().split('.')[1].length;
  } catch (e) {
    r1 = 0;
  }
  try {
    r2 = arg2.toString().split('.')[1].length;
  } catch (e) {
    r2 = 0;
  }
  const c = Math.abs(r1 - r2);
  const m = Math.pow(10, Math.max(r1, r2));
  if (c > 0) {
    const cm = Math.pow(10, c);
    if (r1 > r2) {
      arg1 = Number(arg1.toString().replace('.', ''));
      arg2 = Number(arg2.toString().replace('.', '')) * cm;
    } else {
      arg1 = Number(arg1.toString().replace('.', '')) * cm;
      arg2 = Number(arg2.toString().replace('.', ''));
    }
  } else {
    arg1 = Number(arg1.toString().replace('.', ''));
    arg2 = Number(arg2.toString().replace('.', ''));
  }
  return (arg1 - arg2) / m;
}
