JS 常见的数据操作方法
//四舍五入 function round2(number, fractionDigits) { with (Math) { return round(number * pow(10, fractionDigits)) / pow(10, fractionDigits); } } // 空替换0 function fun_FillStr(Str) { Str = Str.replace(/(^\s*)|(\s*$)/g, "") == "" ? "0" : Str; Str = Str.replace(/(^\s*)|(\s*$)/g, "") == "-" ? "0" : Str; return Str; }
//乘除法 function Fun_Interceptiont(arg1, arg2, type) { var m = 0; var s1 = ""; var s2 = ""; var s3 = ""; try { s1 = arg1.toString(); } catch (e) { } try { s2 = arg2.toString(); } catch (e) { } try { s3 = arg3.toString(); } catch (e) { } try { m += s1.split(".")[1].length; } catch (e) { } try { m += s2.split(".")[1].length; } catch (e) { } if (type == 1) //除法 { JSHJ = Number(s1.replace(".", "")) / Number(s2.replace(".", "")) / Math.pow(10, m); } else { JSHJ = Number(s1.replace(".", "")) * Number(s2.replace(".", "")) / Math.pow(10, m); } } //乘除法 function Fun_Interceptiont_GZ(arg1, arg2, arg3, type) { var m = 0; var s1 = ""; var s2 = ""; var s3 = ""; try { s1 = arg1.toString(); } catch (e) { } try { s2 = arg2.toString(); } catch (e) { } try { s3 = arg3.toString(); } catch (e) { } try { m += s1.split(".")[1].length; } catch (e) { } try { m += s2.split(".")[1].length; } catch (e) { } if (type == 1) //除法 { JSHJ = Number(s1.replace(".", "")) / Number(s2.replace(".", "")) / Math.pow(10, m); } else { JSHJ = Number(s1.replace(".", "")) * Number(s2.replace(".", "")) * Number(s3) / Math.pow(10, m); } } //加减法 function Fun_InterceptiontTotal(arg1, arg2, type) { var r1, r2, m, n; try { r1 = arg1.toString().split('.')[1].length; } catch (e) { r1 = 0; } try { r2 = arg2.toString().split('.')[1].length; } catch (e) { r2 = 0; } if (type == 1) //减法 { m = Math.pow(10, Math.max(r1, r2)); n = (r1 >= r2) ? r1 : r2; JSHJ = ((arg1 * m - arg2 * m) / m).toFixed(n); } else { m = Math.pow(10, Math.max(r1, r2)); JSHJ = (arg1 * m + arg2 * m) / m; } }
//判断字符是否为空的方法 function isEmpty(obj) { if (typeof obj == "undefined" || obj == null || obj == "" || obj == "NaN" || isNaN(obj)) { return true; } else { return false; } } //计算百分比 function GetPercent(num, total) { num = parseFloat(num); total = parseFloat(total); if (isNaN(num) || isNaN(total)) { return "-"; } return total <= 0 ? "0%" : (Math.round(num / total * 10000) / 100.00 + "%"); }
//将小写转化成大写 function atoc(numberValue) { var numberValue = new String(Math.round(numberValue * 100)); // 数字金额 var chineseValue = ""; // 转换后的汉字金额 var String1 = "零壹贰叁肆伍陆柒捌玖"; // 汉字数字 var String2 = "万仟佰拾亿仟佰拾万仟佰拾元角分"; // 对应单位 var len = numberValue.length; // numberValue 的字符串长度 var Ch1; // 数字的汉语读法 var Ch2; // 数字位的汉字读法 var nZero = 0; // 用来计算连续的零值的个数 var String3; // 指定位置的数值 if (len > 15) { alert("超出计算范围"); return ""; } if (numberValue == 0) { chineseValue = "零元整"; return chineseValue; } String2 = String2.substr(String2.length - len, len); // 取出对应位数的STRING2的值 for (var i = 0; i < len; i++) { String3 = parseInt(numberValue.substr(i, 1), 10); // 取出需转换的某一位的值 if (i != (len - 3) && i != (len - 7) && i != (len - 11) && i != (len - 15)) { if (String3 == 0) { Ch1 = ""; Ch2 = ""; nZero = nZero + 1; } else if (String3 != 0 && nZero != 0) { Ch1 = "零" + String1.substr(String3, 1); Ch2 = String2.substr(i, 1); nZero = 0; } else { Ch1 = String1.substr(String3, 1); Ch2 = String2.substr(i, 1); nZero = 0; } } else { // 该位是万亿,亿,万,元位等关键位 if (String3 != 0 && nZero != 0) { Ch1 = "零" + String1.substr(String3, 1); Ch2 = String2.substr(i, 1); nZero = 0; } else if (String3 != 0 && nZero == 0) { Ch1 = String1.substr(String3, 1); Ch2 = String2.substr(i, 1); nZero = 0; } else if (String3 == 0 && nZero >= 3) { Ch1 = ""; Ch2 = ""; nZero = nZero + 1; } else { Ch1 = ""; Ch2 = String2.substr(i, 1); nZero = nZero + 1; } if (i == (len - 11) || i == (len - 3)) { // 如果该位是亿位或元位,则必须写上 Ch2 = String2.substr(i, 1); } } chineseValue = chineseValue + Ch1 + Ch2; } if (String3 == 0) { // 最后一位(分)为0时,加上“整” chineseValue = chineseValue + "整"; } return chineseValue; }