js大数字转换,将大额数字转换为万、千万、亿等

该博客详细介绍了如何编写一个JavaScript函数,用于将大额数值转换为带有中文单位的字符串,如千、万、亿等。函数通过迭代判断数值大小来确定适用的单位,并根据数值精度进行四舍五入处理。适用于前端开发中数值展示的场景。
摘要由CSDN通过智能技术生成

记录一下大额数据转换,

bigNumberTransform (value) {
				const newValue = ['', '', '']
				let fr = 1000
				let num = 3
				let text1 = ''
				let fm = 1
				while (value / fr >= 1) {
					fr *= 10
					num += 1
					// console.log('数字', value / fr, 'num:', num)
				}
				if (num <= 4) { // 千
					newValue[0] = parseInt(value / 1000) + ''
					newValue[1] = '千'
				} else if (num <= 8) { // 万
					text1 = parseInt(num - 4) / 3 > 1 ? '千万' : '万'
					// tslint:disable-next-line:no-shadowed-variable
					fm = text1 === '万' ? 10000 : 10000000
					if (value % fm === 0) {
						newValue[0] = parseInt(value / fm) + ''
					} else {
						newValue[0] = parseFloat(value / fm).toFixed(2) + ''
					}
					newValue[1] = text1
				} else if (num <= 16) { // 亿
					text1 = (num - 8) / 3 > 1 ? '千亿' : '亿'
					text1 = (num - 8) / 4 > 1 ? '万亿' : text1
					text1 = (num - 8) / 7 > 1 ? '千万亿' : text1
					// tslint:disable-next-line:no-shadowed-variable
					fm = 1
					if (text1 === '亿') {
						fm = 100000000
					} else if (text1 === '千亿') {
						fm = 100000000000
					} else if (text1 === '万亿') {
						fm = 1000000000000
					} else if (text1 === '千万亿') {
						fm = 1000000000000000
					}
					if (value % fm === 0) {
						newValue[0] = parseInt(value / fm) + ''
					} else {
						newValue[0] = parseFloat(value / fm).toFixed(2) + ''
					}
					newValue[1] = text1
				}
				if (value < 1000) {
					newValue[0] = value + ''
					newValue[1] = ''
				}
				console.log(newValue.join(''))
				console.log('111111111111')
				return newValue.join('')
			}

参考原文:https://blog.csdn.net/qq_40745143/article/details/112461588

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值