金额加千分位或格式化数据

目录

使用插件:vue-text-format

自定义方法 


使用插件:vue-text-format

 vue-text-format - npmvue-text-format - npmUsed in projects, in accordance with Excel custom format rules.. Latest version: 1.2.6, last published: 3 years ago. Start using vue-text-format in your project by running `npm i vue-text-format`. There are no other projects in the npm registry using vue-text-format.https://www.npmjs.com/package/vue-text-format

安装

npm install vue-text-format

在入口文件引用插件main.js

import Vue from 'vue'


import format from 'vue-text-format';
Vue.use(format);

使用

  <span v-format="'#,##0.00'">123456</span>

转换前:123456

转换后:123,456.00

自定义方法 

 
export function thousands(value) {
    if (!value) return 0
    // 获取整数部分
    const intPart = Math.trunc(value)
    // 整数部分处理,增加,
    const intPartFormat = intPart.toString().replace(/(\d)(?=(?:\d{3})+$)/g, '$1,')
    // 预定义小数部分
    let floatPart = ''
    // 将数值截取为小数部分和整数部分
    const valueArray = value.toString().split('.')
    if (valueArray.length === 2) {
        // 有小数部分
        floatPart = valueArray[1].toString() // 取得小数部分
        return intPartFormat + '.' + floatPart
    }
    return intPartFormat + floatPart
}

使用

在页面中引入写这个方法的js

import { thousands } from '@/utils/auth.js'
for (let i = 0; i < res.data.content.length; i++) {
     res.data.content[i].saleAmount = thousands(res.data.content[i].saleAmount)
}

猜你喜欢

转载自blog.csdn.net/m0_69502730/article/details/130121854