给数字添加分隔逗号,3个一组:如下
C#
代码
#region 把数字用逗号隔开(每三位为一组)
public static string NumberStr(string number)
{
if (!string.IsNullOrEmpty(number))
return Double.Parse(number).ToString("n");
else
return "";
}
#endregion
public static string NumberStr(string number)
{
if (!string.IsNullOrEmpty(number))
return Double.Parse(number).ToString("n");
else
return "";
}
#endregion
输入 123456.78
输出 123,456.78
javascript
代码
function formatNum(num){
if(!/^(\+|-)?(\d+)(\.\d+)?$/.test(num)){alert("wrong!"); 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;
}
if(!/^(\+|-)?(\d+)(\.\d+)?$/.test(num)){alert("wrong!"); 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;
}