JS 數字格式千分位相互轉換
阿新 • • 發佈:2019-02-12
/**
* 數字格式轉換成千分位
*@param{Object}num
*/
function commafy(num){
if((num+"").Trim()==""){
return"";
}
if(isNaN(num)){
return"";
}
num = num+"";
if(/^.*\..*$/.test(num)){
varpointIndex =num.lastIndexOf(".");
varintPart = num.substring(0,pointIndex);
varpointPart =num.substring(pointIndex+1,num.length);
intPart = intPart +"";
var re =/(-?\d+)(\d{3})/
while(re.test(intPart)){
intPart =intPart.replace(re,"$1,$2")
}
num = intPart+"."+pointPart;
}else{
num = num +"";
var re =/(-?\d+)(\d{3})/
while(re.test(num)){
num =num.replace(re,"$1,$2")
}
}
return num;
}
/**
* 去除千分位
*@param{Object}num
*/
function delcommafy(num){
if((num+"").Trim()==""){
return"";
}
num=num.replace(/,/gi,'');
returnnum;
}
* 數字格式轉換成千分位
*@param{Object}num
*/
function commafy(num){
if((num+"").Trim()==""){
return"";
}
if(isNaN(num)){
return"";
}
num = num+"";
if(/^.*\..*$/.test(num)){
varpointIndex =num.lastIndexOf(".");
varintPart = num.substring(0,pointIndex);
varpointPart =num.substring(pointIndex+1,num.length);
intPart = intPart +"";
var re =/(-?\d+)(\d{3})/
while(re.test(intPart)){
intPart =intPart.replace(re,"$1,$2")
}
num = intPart+"."+pointPart;
}else{
num = num +"";
var re =/(-?\d+)(\d{3})/
while(re.test(num)){
num =num.replace(re,"$1,$2")
}
}
return num;
}
/**
* 去除千分位
*@param{Object}num
*/
function delcommafy(num){
if((num+"").Trim()==""){
return"";
}
num=num.replace(/,/gi,'');
returnnum;
}