// 数字相关

// 去掉小数点后面无用的0
function trimZero(num: any) {
  if (num === undefined || num === null) return ''
  let str = `${num}`
  if (!str.includes('.')) {
    // 没有小数点，直接返回
    return str
  }
  while (str.endsWith('0')) {
    str = str.slice(0, -1)
  }
  if (str.endsWith('.')) {
    str = str.slice(0, -1)
  }
  return str
}

function toFixed2(num: any) {
  num = Number(num)
  if (Number.isNaN(num)) return ''
  return trimZero(num.toFixed(2))
}

function toPercent(val: any) {
  val = Number(val)
  if (Number.isNaN(val)) return ''
  return `${toFixed2(val * 100)}%`
}

/**
 * 是否为正整数
 */
function isPositiveInt(val: any) {
  val = String(val)
  if (val.includes('.')) return false
  const numVal = Number(val)
  return !Number.isNaN(numVal) && numVal > 0
}

export default {
  trimZero,
  toFixed2,
  toPercent,
  isPositiveInt,
}
