创建一个filter.js
千分位分割:有小数点保留两位小数反之只进行分隔
let NumFormat = num => {
if (num != undefined) {
let result = (num.toString()).indexOf('.');
if (num == '--') {
return '--'
} else {
if (result != -1) {
return (+num || 0).toFixed(2).toString().replace(/^-?\d+/g, m => m.replace(/(?=(?!\b)(\d{3})+$)/g, ','))
} else {
return (+num || 0).toString().replace(/^-?\d+/g, m => m.replace(/(?=(?!\b)(\d{3})+$)/g, ','))
}
}
}
}
export default { NumFormat }
在main.js里面引入
import filter from '@/lib/filter'
全局导入过滤器
Object.keys(filter).forEach(key => Vue.filter(key, filter[key]))