<html>
<head>
<script>
function a() {
var amount = "-22334.334455";
//if(/(\d*)(\d{3})/.test(amount)) {
// alert(RegExp.$1 + "," + RegExp.$2);
//}
alert(formatAmt(amount));
}
//金额格式化,整数部分每3位用逗号分隔,支持带有正负号以及小数部分
function formatAmt(amt){
if (amt.length <= 3) {
return amt;
}
if(!/^(\+|-)?(\d+)(\.\d+)?$/.test(amt)) {
return amt;
}
var a = RegExp.$1, b = RegExp.$2, c = RegExp.$3;
var re = new RegExp();
re.compile("(\\d)(\\d{3})(,|$)");
while(re.test(b)) {
b = b.replace(re, "$1,$2$3");
}
return a +""+ b +""+ c;
}
</script>
</head>
<body οnlοad="a();">
</body>
</html>