数据'显示'小数格式控制


/**
 * kevin 2021/2/22
 * @description 数据'显示'小数格式控制,
 * @param param
 * @param num 保留小数位数只能保留2位,3位,4位,5位,6位 默认2位
 * @param bool 默认为false不自动补全小数
 * @returns {string | null}
 */
export function pricePoint(param, num, bool) {
  let f_x = parseFloat(param)
  if (isNaN(f_x)) {
    return 0
  }
  if (num === 3) {
    f_x = Math.round(param * 1000) / 1000
  } else if (num === 4) {
    f_x = Math.round(param * 10000) / 10000
  } else if (num === 5) {
    f_x = Math.round(param * 100000) / 100000
  } else if (num === 6) {
    f_x = Math.round(param * 1000000) / 1000000
  } else {
    f_x = Math.round(param * 100) / 100
  }
  if (bool) {
    let s_x = f_x.toString()
    let pos_decimal = s_x.indexOf('.')
    if (pos_decimal <= 0) {
      pos_decimal = s_x.length
      s_x += '.'
    }
    while (s_x.length <= pos_decimal + num) {
      s_x += '0'
    }
    return s_x
  } else {
    return f_x
  }
}

 
 本文出自于https://www.cnblogs.com/sws-kevin/p/15267369.html 转载请注明出处,否则会追究。
posted @ 2022-02-21 10:04  吃不棒的胖胖糖  阅读(31)  评论(0编辑  收藏  举报