//环比值=（当日营业额-昨日营业额）/昨日营业额*100%计算
export const calc_hb_str = function (cur, pre) {
  if (cur == 0 || pre == 0){
    return '0.00%'
  }
  return conver_num_rate(formater((cur - pre) / pre * 100));
};

//同比值=（当日营业额上个月同一日营业额）/上个月同一日营业额*100%计算
export const calc_tb_str = function (cur, pre) {
  return conver_num_rate(formater(cur / pre * 100));
};

//计算占比
export const calc_zb_str = function (item, total) {
  if (item == 0 || total == 0){
    return '0.00%'
  }
  return formater(item / total * 100) + '%';
};


//conver amont
export const conver_amont = function (amont) {
  return formater(amont / 10000);
};

//Positive and negative
export const conver_num_rate = function (amont) {
  if (amont > 0) {
    return "+" + amont + "%"
  } else {
    return amont + "%"
  }
};

/**
 * 保留两位小数
 * @param amont
 * @returns {string|*}
 */
export const conver_num_retain = function (amont) {
  return formater(amont)
}

export const convert_num_unit = function (params) {
  return formater(params.data) + "万元"
}

//数字金额显示
function formater(data) {
  if (!data) return '0.00'
  // 将数据分割，保留两位小数
  data = data.toFixed(2)
  // 获取整数部分
  const intPart = Math.trunc(data)
  // 整数部分处理，增加,
  const intPartFormat = intPart.toString().replace(/(\d)(?=(?:\d{3})+$)/g, '$1,')
  // 预定义小数部分
  let floatPart = '.00'
  // 将数据分割为小数部分和整数部分
  const newArr = data.toString().split('.')
  if (newArr.length === 2) { // 有小数部分
    floatPart = newArr[1].toString() // 取得小数部分
    return intPartFormat + '.' + floatPart
  }
  return intPartFormat + floatPart
}
