/* * formatMoney(s,type) * 功能:金額按千位逗號分割 * 參數(shù):s,需要格式化的金額數(shù)值. * 參數(shù):type,判斷格式化后的金額是否需要小數(shù)位. * 返回:返回格式化后的數(shù)值字符串. */ function formatMoney(s, type) { if (/[^0-9\.]/.test(s)) return "0"; if (s == null || s == "") return "0"; s = s.toString().replace(/^(\d*)$/, "$1."); s = (s + "00").replace(/(\d*\.\d\d)\d*/, "$1"); s = s.replace(".", ","); var re = /(\d)(\d{3},)/; while (re.test(s)) s = s.replace(re, "$1,$2"); s = s.replace(/,(\d\d)$/, ".$1"); if (type == 0) {// 不帶小數(shù)位(默認是有小數(shù)位) var a = s.split("."); if (a[1] == "00") { s = a[0]; } } return s; }
|