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

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/fightWz/article/details/84136786
/**
 * 数字格式化金钱展示
 * @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(',', ''))
    }
  }
}

猜你喜欢

转载自blog.csdn.net/fightWz/article/details/84136786