子钦加油

扩大
缩小

数字(金钱格式)相互转化

/**
 * 数字格式化金钱展示
 * @param {*} num 串数字
 * @returns
 */
export const numFormat = (num) => {
  if (typeof (num) != 'number') {
    num = Number(num)
  }
  num = num.toFixed(2);
  num = parseFloat(num)
  num = num.toLocaleString();
  let floatPart = '.00' // 预定义小数部分
  let numArry = num.split('.')
  // =2表示数据有小数位
  if (numArry.length === 2) {
    floatPart = numArry[1].toString() // 拿到小数部分
    if (floatPart.length === 1) { // 补0,实际上用不着
      return numArry[0] + '.' + floatPart + '0'
    } else {
      return numArry[0] + '.' + floatPart
    }
  } else {
    return num + floatPart
  }
}

/**
 *
 * 金钱格式化数字
 * @param {*} 传字符串或者数字
 * @returns
 */
export const number = (value) => {
  if (typeof (value) == 'number') {
    return value
  } else {
    if (value.indexOf(',') != -1) {
      return Number(value)
    } else {
      return Number(value.replace(',', ''))
    }
  }
}

 

 

posted on 2019-11-27 23:23  子钦加油  阅读(513)  评论(0编辑  收藏  举报

导航

返回顶部