1. C#方法
string Fmt2 = "#,###.00"; string Fmt0 = "#,###"; /// <summary> /// 格式化數量 /// </summary> private string FormatQty(string qty) { // 數量為0 if (qty.Equals("0")) return "0"; double d = 0D; if (qty.IndexOf('.') > -1) { // 有小數 if (double.TryParse(qty, out d)) return d.ToString(Fmt2); } else { // 無小數 if (double.TryParse(qty, out d)) return d.ToString(Fmt0); } // reutrn string.Empty; return FormatNullOrEmpty(string.Empty); }
2. JavaScript方法(来自网络)
function formatThousandStyle(number) { var num = number + ""; num = num.replace(new RegExp(",","g"),""); // 正负号处理 var symble = ""; if(/^([-+]).*$/.test(num)) { symble = num.replace(/^([-+]).*$/,"$1"); num = num.replace(/^([-+])(.*)$/,"$2"); } if(/^[0-9]+(\.[0-9]+)?$/.test(num)) { var num = num.replace(new RegExp("^[0]+","g"),""); if(/^\./.test(num)) { num = "0" + num; } var decimal = num.replace(/^[0-9]+(\.[0-9]+)?$/,"$1"); var integer= num.replace(/^([0-9]+)(\.[0-9]+)?$/,"$1"); var re=/(\d+)(\d{3})/; while(re.test(integer)){ integer = integer.replace(re,"$1,$2"); } return symble + integer + decimal; } else { return number; } }