vue通过filter过滤器格式化金额

  1. numFormat.js
const digitsRE = /(\d{3})(?=\d)/g;

export default (value, currency = '元', decimals = 2) => {
  value = parseFloat(value);
  if (!value && value !== 0) return '';
  const stringified = Math.abs(value).toFixed(decimals);
  const $int = decimals ? stringified.slice(0, -1 - decimals) : stringified;
  const i = $int.length % 3;
  const head = i > 0 ? ($int.slice(0, i) + ($int.length > 3 ? ',' : '')) : '';
  const $float = decimals ? stringified.slice(-1 - decimals) : '';
  const sign = value < 0 ? '-' : '';
  return `${sign}${head}${$int.slice(i).replace(digitsRE, '$1,')}${$float} ${currency}`;
};

  1. mainjs全局注册
import numFormat from '@/libs/numFormat';
// 注册金额格式化过滤器
Vue.filter('numFormat', numFormat);
  1. 使用
{{item.outsourceAmount | numFormat}}
  1. 效果图


    image.png

你可能感兴趣的:(vue通过filter过滤器格式化金额)