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

js中如何把9999转换成金额格式,即9,999
js中如何把9999转换成金额格式,即9,999

------解决方案--------------------
//引用梅花雪的代码
//将数字转换成三位逗号分隔的样式
function formatNum(s)
{
if(!/^(\+|-)?\d+(\.\d+)?$/.test(num)){alert( "wrong! "); return num;}
var re = new RegExp().compile( "(\\d)(\\d{3})(,|\\.|$) ");
num += " ";
while(re.test(num))
num = num.replace(re, "$1,$2$3 ")
return num;
}
alert(formatNum(-12345678.123));
alert(formatNum( "12345678.123 "));
alert(formatNum( "10000000000000000000000000000000000000000 "));