js 金额格式化-千位分隔符

function submoney(value){
	var b = false;
	if (value == null || value == "") return "0"; 
	value = value.toString();
	if(value.indexOf('-') != -1){
		b= true;
		value = value.substring(1,value.length);
	}
	if (/^\-?[0-9]+(.[0-9]+)?$/.test(value)){
		value = value.toString().replace(/^(\d*)$/, "$1."); 
		value = (value + "00").replace(/(\d*\.\d\d)\d*/, "$1"); 
		value = value.replace(".", ","); 
		var re = /(\d)(\d{3},)/; 
		while (re.test(value)) 
			value = value.replace(re, "$1,$2"); 
		value = value.replace(/,(\d\d)$/, ".$1"); 
	}
	if(b){
		value = "-"+value;
	}
	return value;
}
posted @ 2021-07-30 15:49  helloSWZ  阅读(453)  评论(0编辑  收藏  举报