案例图
封装的方法
// 数字格式化,千分位显示格式
export const formatPrice = (s, n) => {
if (s == null) return s //为null 返回空
n = n > 0 && n <= 20 ? n : 2;
s = parseFloat((s + '').replace(/[^\d\.-]/g, '')).toFixed(n) + '';
var l = s.split('.')[0].split('').reverse();
var r = s.split('.')[1];
var t = '';
for (var i = 0; i < l.length; i++) {
t += l[i] + ((i + 1) % 3 == 0 && (i + 1) != l.length && l[i + 1] != '-' ? ',' : '');
}
return t.split('').reverse().join('') + '.' + r;
}
使用的方法
// // 使用方法如下
// // 直接使用前提是必须在main.js中全局注册
// // import { formatPrice } from "@/utlis/index.js";
// // 全局注册金额格式化
// // Vue.filter('formatPrice', function(dateStr) {
// // // 在过滤器的处理函数中,最后,必须 return 一个东西,这样,才是一个合法的过滤器
// // return formatPrice(dateStr)
// // })
// // 页面直接使用
// // {
{`获取的数字` | formatPrice}} 页面显示时间的位置