index.js 915 Bytes

/**
   * 金额单位转换
   * @param {Number} value
   * @param {Number} format
   */
const moneyFormat = (value, format) => {
  if (value === '' || value === null || value === undefined) {
    return '--'
  }
  if (format === 4) { // 转万元
    return (Number(value) / 10000).toFixed(0).toLocaleString()
  }
  if (format === 8) { // 转亿元
    return (Number(value) / 10000 / 10000).toFixed(0).toLocaleString()
  }
  if (format === 8.5) { // 转亿元
    return (Number(value) / 10000 / 10000).toLocaleString()
  }
}
/**
   * 千分位符转换
   * @param {Number} num
   */
const numFormat = (num) => {
  if (num === '--' || num === '' || num === null || num === undefined) {
    return '--'
  }
  var _num = Number(num)
  var c = (_num.toString().indexOf('.') !== -1) ? _num.toLocaleString() : _num.toString().replace(/(\d)(?=(?:\d{3})+$)/g, '$1,')
  return c
}
export {
  moneyFormat,
  numFormat
}