vue 纯js实现千位分隔符显示

<temolate>
	<span class="left-con-item">金额:<template>{{transLateToThousand(5420000)}}span>template>
export default{
	data(){
		return {
		}
	},
	methods:{
		transLateToThousand (val) {//val就是要格式化的值
		  if (val) {
		    val = typeof val === 'string' ? val : String(val)
		    if (val.split('.')[0].length > 3) {
		      var transStr = val.split('.')[0].substring(val.split('.')[0].length % 3)
		      var arr = []
		      for (var i = 0; i < transStr.length; i = i + 3) {
		        arr.push(transStr.substring(i, i + 3))
		      }
		      var res = ''
		      if (val.split('.')[0].length % 3 > 0) {
		        res = `${val.split('.')[0].substring(0, val.split('.')[0].length % 3)},${arr.join()}.${val.split('.')[1]}`
		      } else {
		        res = `${arr.join()}.${val.split('.')[1]}`
		      }
		      return res
		    } else {
		      return val
		    }
		  }
		}
	}
}

你可能感兴趣的:(javascript,vue.js,前端)