<HTML>
<BODY>
<script language=javascript>
function commafy(num)
{
num = num+"";
var re=/(-?\d+)(\d{3})/
while(re.test(num))
{
num=num.replace(re,"$1,$2")
}
return num;
}
function commafyback(num)
{
var x = num.split(',');
return parseFloat(x.join(""));
}
function formatNumber(num){
if(!/^(\+|-)?(\d+)(\.\d+)?$/.test(num)){return num;}
var a = RegExp.$1, b = RegExp.$2, c = RegExp.$3;
var re = new RegExp().compile("(\\d)(\\d{3})(,|$)");
while(re.test(b)) b = b.replace(re,"$1,$2$3");
return a +""+ b +""+ c;
}
</script>
<input type=button value="commafy" onclick="alert(commafy(123456789.01))">
<input type=button value="commafy" onclick="alert(commafyback('123,456,789.01'))">
</BODY>
</HTML>
join()默认是以逗号分隔。
当然程序还可以写的多一点点,比如split 后,用 for in实现链接
转自:http://www.blogjava.net/JAVA-HE/archive/2007/06/20/125225.html