/**
* Desc:数字千分位、小数点后 ${fixedNum} 位
*/
export function numberFormat(v, fixedNum = 2){
let n = typeof v == 'string' ? Number(v) : v;
return n.toFixed(fixedNum).toString().replace(/(\d{1,3})(?=(\d{3})+(?:$|\.))/g, "$1,")
}
将上面文件保存为 tools.js,在 vue 中作为 filters 使用:
<template>
<div class="app-container">
{{ 6666666 | numberFormat }}
</div>
</template>
<script>
import {numberFormat} from "@/utils/tools"
export default {
filters: {numberFormat}
}
</script>
<style scoped lang="scss">
@import "@/assets/styles/style-lhy";
</style>