Skip to main content

格式化金额

/** * 格式化金额 千分符 * @param value * @param fixed */export function transformThousandth(value: number | string, fixed?: number): string {  const needFixed = fixed != null  const num = Number(value)  if (isNaN(num)) {    return needFixed ? (0).toFixed(fixed) : '0'  }  // return (needFixed ? num.toFixed(fixed) : num.toString()).replace(/(\d{1,3})(?=(\d{3})+(?:$|\.))/g, '$1,')  const str = needFixed ? num.toFixed(fixed) : num.toString()  const arr = str.split('.')
  let result = arr[0] ? arr[0].replace(/(?=(?!\b)(\d{3})+$)/g, ',') : '0'  if (arr[1] != null) {    result += `.${arr[1]}`  }
  return result}
// 校验是否是金额格式export function isMoneyFormat(money: number | string): boolean {  if (typeof money === 'number') {    return money === money  }  const re1 = /^0\.[\d]{0,2}$/  const re2 = /^[1-9][\d]*(\.[\d]{0,2})?$/  const re3 = /^0$/  return re1.test(money) || re2.test(money) || re3.test(money)}