正则表达式:数值及数值型字符串三位一组格式化

<body>
<div id="container"></div>
<script>
var formatNumber = function(str,split){
		var type = typeof str;
		switch (type){
			case "number":
				str = str.toString();
			case "string":
				return str.replace(/(\d+?)(?=(?:\d{3})+$)/g,"$1"+split);
				break;
			default:
				return ;
		}		
	},
	arr=["0.3000","22.356","444.456",123456.56,0,"1","111","1211","123456","1457910","aaaa",{"name":"zhangsan"}],
	str="<table width='50%' cellspacing='0' cellpadding='0' border='1' align='center'>";
for(var i = 0; i< arr.length;i++){
	var ss = arr[i],
		format = formatNumber(ss,",");
	str += "<tr><td>"+ss+"</td><td>"+format+"</td></tr>";
}
str+="</table>";
document.getElementById("container").innerHTML = str;
</script>
</body>


结果如下图:

 

posted @ 2013-08-16 18:36  pangbangb  阅读(363)  评论(0编辑  收藏  举报