
/**
 * 元换算为万元
 * @param num ：
 * @returns {number} ：
 */
function divTenThousand (num) {
  if (num == null) {
    return 0
  }
  return div(num, 10000, 3)
  // return num
}

/**
 * 元换算为万元
 * @param num ：
 * @returns {RegExpMatchArray} ：
 */
function divScale (num) {
  if (num == null) {
    return 0
  }
  const str = Number(Math.round(num * 100) / 100).toString()
  if (str.indexOf('.') > 0) {
    return (str + '00').match(/^\d+(?:\.\d{0,2})?/)
  }
  return str + '.00'
  // return num
}

/**
 * 元换算为万元，并格式化
 * @param num ：
 * @returns {string} ：
 */
// eslint-disable-next-line no-unused-vars
function divTenThousandFormatter (num) {
  return formatter(divTenThousand(num))
}

function formatter (value) {
  if (value == null) {
    return value
  }
  return value.replace(/\B(?=(\d{3})+(?!\d))/g, ',')
}

// eslint-disable-next-line no-unused-vars
function parser (value) {
  return value.replace(/\$\s?|(,*)/g, '')
}

/**
 * 万元换算为元
 * @param num
 * @returns {*}
 */
// eslint-disable-next-line no-unused-vars
function mulTenThousand (num) {
  if (num == null || num === '') {
    return 0
  }
  return mul(num, 10000, 6)
  // return num
}

// eslint-disable-next-line no-unused-vars
function add (arg1, arg2) {
  if (arg1 == null || arg1 === '') {
    arg1 = 0
  }
  if (arg2 == null || arg2 === '') {
    arg2 = 0
  }
  let r1, r2
  try {
    r1 = arg1.toString().split('.')[1].length
  } catch (e) {
    r1 = 0
  }
  try {
    r2 = arg2.toString().split('.')[1].length
  } catch (e) {
    r2 = 0
  }
  const m = Math.pow(10, Math.max(r1, r2))
  const n = (r1 >= r2) ? r1 : r2
  return ((arg1 * m + arg2 * m) / m).toFixed(n)
}

// eslint-disable-next-line no-unused-vars
function sub (arg1, arg2) {
  if (arg1 == null || arg1 === '') {
    arg1 = 0
  }
  if (arg2 == null || arg2 === '') {
    arg2 = 0
  }
  let re1, re2
  try {
    re1 = arg1.toString().split('.')[1].length
  } catch (e) {
    re1 = 0
  }
  try {
    re2 = arg2.toString().split('.')[1].length
  } catch (e) {
    re2 = 0
  }
  const m = Math.pow(10, Math.max(re1, re2))
  const n = (re1 >= re2) ? re1 : re2
  return ((arg1 * m - arg2 * m) / m).toFixed(n)
}

/**
 * 1
 * @param arg1
 * @param arg2
 * @returns {number}
 */
// eslint-disable-next-line no-unused-vars
function mul (arg1, arg2) {
  if (arg1 == null || arg1 === '') {
    arg1 = 0
  }
  if (arg2 == null || arg2 === '') {
    arg2 = 0
  }
  let m = 0
  const s1 = arg1.toString()
  const s2 = arg2.toString()
  try {
    m += s1.split('.')[1].length
  } catch (e) {}
  try {
    m += s2.split('.')[1].length
  } catch (e) {}

  return Number(s1.replace('.', '')) * Number(s2.replace('.', '')) / Math.pow(10, m)
}
/**
 * 除法 注意如果整除了 小数点将无效
 * @param arg1 ： 被除数
 * @param arg2 ： 除数
 * @param digit ：精确位数
 * @returns {number}
 */
function div (arg1, arg2, digit) {
  if (arg1 == null || arg1 === '') {
    arg1 = 0
  }
  if (arg2 == null || arg2 === '') {
    arg2 = 1
  }
  let t1 = 0
  let t2 = 0
  try { t1 = arg1.toString().split('.')[1].length } catch (e) {}
  try { t2 = arg2.toString().split('.')[1].length } catch (e) {}
  const r1 = Number(arg1.toString().replace('.', ''))
  const r2 = Number(arg2.toString().replace('.', ''))
  // 获取小数点后的计算值
  const result = ((r1 / r2) * Math.pow(10, t2 - t1)).toString()
  let result2 = result.split('.')[1]
  if (result2 !== undefined) {
    result2 = result2.substring(0, digit > result2.length ? result2.length : digit)
  } else {
    result2 = ''
  }
  while (result2.length < digit) {
    result2 = result2 + '0'
  }
  if (digit === 0) {
    return Number(result.split('.')[0])
  }
  return Number(result.split('.')[0] + '.' + result2)
}
