JS系列--【千分位处理和还原】

1.千分位转化文件 utils/common.js

const toThousands = function (value, num = 0) {
    if (value == null) {
        return
    }
    if(value>0){
        value *= 1;
        if(value>=1000){
            return (parseFloat(value).toFixed(num)).replace(/\d{1,3}(?=(\d{3})+(\.\d*)?$)/g, '$&,')

        }else{
            return parseFloat(value)
        }
    }else{
        value *= -1;
        if(value>=1000){
            return (parseFloat(value*-1).toFixed(num)).replace(/\d{1,3}(?=(\d{3})+(\.\d*)?$)/g, '$&,')

        }else{
            return parseFloat(value*-1)
        }

    }

}
const reduction = function(str){
    if(str == ''){
        return
    }
    return parseFloat(str.replace(/[^\d\.-]/g,""))   
}
 export {
    toThousands,
    reduction
 }

2.使用的组件引入文件

import {reduction,toThousands } from "../utils/common.js"

使用
reduction(val)

3.如果需要全局使用的话,在main.js添加到全局过滤器上

import {toThousands} from "../untils/common.js"
vue.filter("toThousandsFilter", function(value,num){
  return toThousands(value,num)
})

4.全局使用

{{row.[item.key] |toThousandsFilter}}

 

posted on 2023-03-08 21:01  码农小小海  阅读(35)  评论(0编辑  收藏  举报

导航