JavaScript 数字千元符方法

// normal 参数是币种 币种等于 JPY 【日元】不显示千元符 默认显示千元符
function toThousands(num = 0, currency = 'normal', point = 2) {
  let newNum = String(num)
  var reg = new RegExp(',', 'g')
  if (newNum.indexOf(',') !== -1) {
    newNum = newNum.replace(reg, '')
  }
  let zero = '.00'
  if (newNum === 'null') {
    newNum = 0
  } else {
    newNum = Number(newNum)
  }
  if (currency === 'JPY') zero = ''
  var [sInt, sFloat] = (Number.isInteger(newNum) ? `${newNum}` : newNum.toFixed(point)).split('.')
  sInt = sInt.replace(/\d(?=(\d{3})+$)/g, '$&,')
  return sFloat ? `${sInt}.${sFloat}` : `${sInt}` + `${zero}`
}

 

上一篇:mysql 5.7.28 中GROUP BY报错问题 SELECT list is not in GROUP BY clause and contains no


下一篇:2022.1.20 考试总结+题解