vue实现数字千分位格式化 如6,383,993,037,937.463

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);
},
相关推荐
勿语&15 分钟前
Element-UI Plus 暗黑主题切换及自定义主题色
开发语言·javascript·ui
黄尚圈圈17 分钟前
Vue 中引入 ECharts 的详细步骤与示例
前端·vue.js·echarts
一路向前的月光5 小时前
Vue2中的监听和计算属性的区别
前端·javascript·vue.js
长路 ㅤ   5 小时前
vue-live2d看板娘集成方案设计使用教程
前端·javascript·vue.js·live2d
Fan_web5 小时前
jQuery——事件委托
开发语言·前端·javascript·css·jquery
Jiaberrr6 小时前
Element UI教程:如何将Radio单选框的圆框改为方框
前端·javascript·vue.js·ui·elementui
安冬的码畜日常8 小时前
【D3.js in Action 3 精译_029】3.5 给 D3 条形图加注图表标签(上)
开发语言·前端·javascript·信息可视化·数据可视化·d3.js
太阳花ˉ8 小时前
html+css+js实现step进度条效果
javascript·css·html
程序员大金9 小时前
基于SpringBoot+Vue+MySQL的装修公司管理系统
vue.js·spring boot·mysql