1、js部分
import Vue from 'vue' Vue.filter('money', function(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 (var 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); })
2、html 部分
<template> <div id="box"> <p style="text-align:center;">总金额: {{ this.money | money}}</p> </div> </template> <script> import './filter/filter.js' export default { data () { return { money: 1000000.89 } }, } </script>
所有评论(0)