JavaScript 金额元转化为万

function dealNum(price){
    if (price === 0) {
      return '0元'
    }
    const BASE = 10000
    const decimal = 0
    const SIZES = ["", "万", "亿", "万亿"];
    let i = undefined;
    let str = "";

    if (price) {
      if ((price > 0 && price < BASE) || (price < 0 && price > -10000)) {
        str = `${Math.round(price)}`
      } else if (price < 0) {
        let num = Math.abs(price)
        i = Math.floor(Math.log(num) / Math.log(BASE))
        str = `${((num / Math.pow(BASE, i))).toFixed(decimal)}${SIZES[i]}`
        str = `-${str}`
      } else {
        i = Math.floor(Math.log(price) / Math.log(BASE))
        str = `${((price / Math.pow(BASE, i))).toFixed(decimal)}${SIZES[i]}`
      }
      return str
    } else {
      return '--'
    }
}

运行结果:
JavaScript 金额元转化为万_第1张图片

你可能感兴趣的:(#,JavaScript,javascript,开发语言,ecmascript)