前端开发

vue 金额格式化---过滤器

2020-02-29  本文已影响0人  super静_jingjing
export default{
  filters: {
        money(val)
        {
            val = val.toString().replace(/\$|\,/g, "");
            if (isNaN(val))
            {
                val = "0";
            }
            let sign = (val == (val = Math.abs(val)));
            val = Math.floor(val * 100 + 0.50000000001);
            let cents = val % 100;
            val = Math.floor(val / 100).toString();
            if (cents < 10)
            {
                cents = "0" + cents;
            }
            for (let i = 0; i < Math.floor((val.length - (1 + i)) / 3); I++)
            {
                val = val.substring(0, val.length - (4 * i + 3)) + "," + val.substring(val.length - (4 * i + 3));
            }

            return (((sign) ? "" : "-") + val + "." + cents);
        }
    },
    data(){
    },
   mounted(){},
   methods:{}
}

vue页面代码

{{ 1000 | money }}
效果: image.png
上一篇下一篇

猜你喜欢

热点阅读