1.封装文件:numberToCurrency.js
/**实现数字千分位格式化 如6,383,993,037,937.463 */
export function numberToCurrencyNo(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.slice(0,2)}return intPartFormat + floatPart
}
2.在index.vue引用
<template><div><--页面引用-->{{ numberToCurrencyNo(6383993037937.463) }}</div>
</template>import { numberToCurrencyNo } from "@/numberToCurrency.js"; //千分位格式化//js中使用
mounted(){
let dataNew = '6383993037937.463'dataNew=numberToCurrencyNo(data);
},