js将大数字金额转换成带单位的数字金额,万,千万,亿,
第一个参数是在转换的数字,
第二个参数是返回的格式
不传第二个参数,返回数字和单位分开的,数组格式,传第二个参数后,返回数组和单位的字符串,这样的话可以对数字和单位分别做特殊处理,方便对字号,颜色等分开设置
样例:
安装 npm i big-number-transform
使用:
import bigNumberTransform from 'big-number-transform'
const nums = bigNumberTransform(232323)
源码 js版:
let bigNumberTransform = function(value,type) {
const newValue = ['', '', '']
let fr = 1000
let num = 3
let text1 = ''
let text2 = ''
let fm = 1
if(value=='' || value==null || isNaN(value)){
return !type?newValue:''
}
if(value<0){
value = Math.abs(value);
text2 = '-'
}
while (value / fr >= 1) {
fr *= 10
num += 1
// console.log('数字', value / fr, 'num:', num)
}
if (num <= 4) { // 千
newValue[0] = value
newValue[1] = ''
} else if (num <= 8) { // 万
// text1 = parseInt(num - 4) / 3 > 1 ? '千万' : '万'
text1 = '万'
// 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 = '亿'
text1 = (num - 8) / 4 > 1 ? '万亿' : text1
text1 = (num - 8) / 7 > 1 ? '千万亿' : text1
text1 = (num - 8) / 10 > 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
}else{
fm = 1000000000000000000
}
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] = '元'
}
newValue[0] = text2?text2 + newValue[0] : newValue[0]
return !type?newValue:(newValue[0] + newValue[1])
}
源码ts版
// src/index.ts
export default function(value:number,type:string) {
const newValue = ['', '', '']
let fr = 1000
let num = 3
let text1 = ''
let text2 = ''
let fm = 1
if(value==null || isNaN(value)){
return !type?newValue:''
}
if(value<0){
value = Math.abs(value);
text2 = '-'
}
while (value / fr >= 1) {
fr *= 10
num += 1
// console.log('数字', value / fr, 'num:', num)
}
if (num <= 4) { // 千
newValue[0] = value.toString()
newValue[1] = ''
} else if (num <= 8) { // 万
// text1 = parseInt(num - 4) / 3 > 1 ? '千万' : '万'
text1 = '万'
// tslint:disable-next-line:no-shadowed-variable
fm = text1 === '万' ? 10000 : 10000000
if (value % fm === 0) {
newValue[0] = parseInt((value / fm).toString()) + ''
} else {
newValue[0] = parseFloat((value / fm).toString()).toFixed(2) + ''
}
newValue[1] = text1
} else { // 亿 if (num <= 16)
// text1 = (num - 8) / 3 > 1 ? '千亿' : '亿'
text1 = '亿'
text1 = (num - 8) / 4 > 1 ? '万亿' : text1
text1 = (num - 8) / 7 > 1 ? '千万亿' : text1
text1 = (num - 8) / 10 > 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
}else{
fm = 1000000000000000000
}
if (value % fm === 0) {
newValue[0] = parseInt((value / fm).toString()) + ''
} else {
newValue[0] = parseFloat((value / fm).toString()).toFixed(2) + ''
}
newValue[1] = text1
}
if (value < 1000) {
newValue[0] = value + ''
newValue[1] = '元'
}
newValue[0] = text2?text2 + newValue[0] : newValue[0]
return !type?newValue:(newValue[0] + newValue[1])
}