var num1=12345.33
//有小数的 。小数点后面的不用分隔
function fomat(num){
let str=num.toString()
let decimals=''
if(str.indexOf('.')>-1){
let arr=str.split('.')
str=arr[0]
decimals=arr[1]
console.log(decimals)
}
let length=str.length
if(length<=3){
return str
}
let temp=decimals?'.'+decimals:''
let remainder=length%3
if(remainder>0){
return str.slice(0,remainder)+','+str.slice(remainder,length).match(/\d{3}/g).join(',')+temp
}else{
return str.slice(0,length).match(/\d{3}/g).join(',')+temp
}
}
let num2=fomat(num1)
console.log(num2)
var num3=12345678
//没有小数的
function fomat2(num){
let str=num.toString()
let length=str.length
let remainder=length%3
if(length<=3){
return str
}
if(remainder>0){
return str.slice(0,remainder)+','+str.slice(remainder,length).match(/\d{3}/g).join(',')
}else{
return str.slice(0,length).match(/\d{3}/g).join(',')
}
}
let num4=fomat(num3)
console.log(num4)
console