<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(""));
}
</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实现链接。
posted on 2007-06-20 01:19
-274°C 阅读(3274)
评论(3) 编辑 收藏