js格式化金额
js格式化金额
1.格式化金额精确到小数点后两位,如:123.00
// 订单金额格式化 精确到2位 getNumberFloat: function(moneyNum) { var f = parseFloat(moneyNum); if (isNaN(f)) { return '0.00'; } var s = f.toString(); var rs = s.indexOf('.'); if (rs < 0) { //没有小数点 rs = s.length; s += '.'; } if (s.length - (rs + 1) > 2) { //表示小数点大于2位 return s.substr(0, rs + 3); } while (s.length <= rs + 2) { s += '0'; } return s; },
2.格式化金额,每3位数隔开,如10000==10,000
//格式化金额,如10000==10,000 formatMoneyNumber: function(moneyNum) { var result = isNaN((1 * moneyNum).toFixed(2)) ? (new Number( 0).toFixed(2)) : (1 * moneyNum).toFixed(2); if (typeof(result) == "number") { result = result + ".00"; } return /\./.test(result) ? result.replace( /(\d{1,3})(?=(\d{3})+\.)/g, "$1,") : result .replace(/(\d{1,3})(?=(\d{3})+\b)/g, "$1,"); },
3.格式化金额,转为正常数字,如10,000==10000
//金钱格式化成数字,如10,000==10000 moneyToNumValue: function(moneyNum) { var num = moneyNum.trim(); var ss = num.toString(); if (ss.length == 0) { return "0"; } return ss.replace(/,/g, ""); },