日期:2014-05-16  浏览次数:20343 次

js对数值千分位处理的方法
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <title> New Document </title>
  <meta name="Generator" content="EditPlus">
  <meta name="Author" content="">
  <meta name="Keywords" content="">
  <meta name="Description" content="">
 </head>
<script type="text/javascript">
<!--
//-------------------------------------------- 
//删除千分点。 
//-------------------------------------------- 
function removeComma(number) {
	var num = number+"";
	num = num.replace(new RegExp(",","g"),""); 
		if(/^[-+]?[0-9]+(\.[0-9]+)?$/.test(num)) { 
		return num; 
	} else { 
		return number; 
	} 
} 

//-------------------------------------------- 
//添加千分点。 
//-------------------------------------------- 
function addKannma(number) {
	var num = number + "";
	num = num.replace(new RegExp(",","g"),""); 
	// 正负号处理 
	var symble = ""; 
	if(/^([-+]).*$/.test(num)) { 
		symble = num.replace(/^([-+]).*$/,"$1"); 
		num = num.replace(/^([-+])(.*)$/,"$2"); 
	} 

	if(/^[0-9]+(\.[0-9]+)?$/.test(num)) { 
		var num = num.replace(new RegExp("^[0]+","g"),""); 
		if(/^\./.test(num)) { 
		num = "0" + num; 
		} 

		var decimal = num.replace(/^[0-9]+(\.[0-9]+)?$/,"$1"); 
		var integer= num.replace(/^([0-9]+)(\.[0-9]+)?$/,"$1"); 

		var re=/(\d+)(\d{3})/;

		while(re.test(integer)){ 
			integer = integer.replace(re,"$1,$2");
		} 
		return symble + integer + decimal; 

	} else { 
		return number; 
	} 
}

var i = 23423424.1232;
var si = addKannma(i);
var ii = removeComma(si);
alert(si);
alert(ii);

//-->
</script>
 <body>
  
 </body>
</html>

?