js大數字轉換,將大額數字轉換為萬、千萬、億等


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))


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM