zl程序教程

您现在的位置是:首页 >  后端

当前栏目

js数值转换为3位逗号分隔的示例代码

JS转换代码 示例 数值 逗号 分隔
2023-06-13 09:15:18 时间

实例代码:

复制代码代码如下:

functionformatNum(strNum){

if(strNum.length<=3){

returnstrNum;

}

if(!/^(\+|-)?(\d+)(\.\d+)?$/.test(strNum)){

returnstrNum;

}

vara=RegExp.$1,b=RegExp.$2,c=RegExp.$3;

varre=newRegExp();

re.compile("(\\d)(\\d{3})(,|$)");

while(re.test(b)){

b=b.replace(re,"$1,$2$3");

}

returna+""+b+""+c;

}