一、在util文件下创建一个currency.js文件
const digitsRE = /(\d{3})(?=\d)/g
export function currency (value, currency, decimals) {
// value = parseFloat(value)
if (!isFinite(value) || (!value && value !== 0)) return ''
currency = currency != null ? currency : ''
decimals = decimals != null ? decimals : 0
var stringified = Math.abs(value).toFixed(decimals)
var _int = decimals? stringified.slice(0, -1 - decimals): stringified
var i = _int.length % 3
var head = i > 0? (_int.slice(0, i) + (_int.length > 3 ? ',' : '')): ''
var _float = decimals? stringified.slice(-1 - decimals): ''
var sign = value < 0 ? '-' : ''
if(value>=10000){
return value = (value / 10000).toFixed(1) + "万"
}else{
return sign + currency + head +
_int.slice(i).replace(digitsRE, '$1,') +
_float
}
}
二、在组件中引入改文件,定义过滤器
<script>
import {currency} from './util/currency'
export default({
filters:{
currency:currency
},
}
</script>
三、使用的方式
{{money|currency}}
四、效果图如下