首页 > Web开发 > 详细

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

时间:2021-08-13 23:13:56      阅读:24      评论:0      收藏:0      [点我收藏+]
function 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(1) + ‘‘
}
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] = ‘‘
}
return newValue.join(‘‘)
}
console.log(bigNumberTransform(11234111))

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

原文:https://www.cnblogs.com/anans/p/15138838.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!