|
@@ -145,36 +145,64 @@ module.exports = {
|
|
|
rightTrim: function(rst) {
|
|
|
return str.replace(/(\s*$)/g,"");
|
|
|
},
|
|
|
- convertNumToChinese : function(num, isCurrency) {
|
|
|
- if (!/^\d*(\.\d*)?$/.test(num)) { return "Number is wrong!"; }
|
|
|
- let AA, BB;
|
|
|
- if (isCurrency) {
|
|
|
- AA = ["零", "壹", "贰", "叁", "肆", "伍", "陆", "柒", "捌", "玖"];
|
|
|
- BB = ["", "拾", "佰", "仟", "萬", "億", "点", ""];
|
|
|
- } else {
|
|
|
- AA = ['零','一','二','三','四','五','六','七','八','九'];
|
|
|
- BB = ["", "十", "百", "千", "万", "亿", "点", ""];
|
|
|
- }
|
|
|
- //let AA = new Array("零", "壹", "贰", "叁", "肆", "伍", "陆", "柒", "捌", "玖");
|
|
|
- //let BB = new Array("", "拾", "佰", "仟", "萬", "億", "点", "");
|
|
|
- let a = ("" + num).replace(/(^0*)/g, "").split("."), k = 0, re = "";
|
|
|
- for (let i = a[0].length - 1; i >= 0; i--) {
|
|
|
- switch (k) {
|
|
|
- case 0: re = BB[7] + re; break;
|
|
|
- case 4: if (!new RegExp("0{4}\\d{" + (a[0].length - i - 1) + "}$").test(a[0]))
|
|
|
- re = BB[4] + re; break;
|
|
|
- case 8: re = BB[5] + re; BB[7] = BB[5]; k = 0; break;
|
|
|
+ replaceAll: function (targetStr, FindText, RepText) {
|
|
|
+ let regExp = new RegExp(FindText, "gm");
|
|
|
+ return targetStr.replace(regExp, RepText);
|
|
|
+ },
|
|
|
+ convertToCaptionNum: function(num, isCurrency, isTraditionalCap) {
|
|
|
+ let me = this, rst = "";
|
|
|
+ if (/^\d*(\.\d*)?$/.test(num)) {
|
|
|
+ let capChars, unitChars;
|
|
|
+ if (isTraditionalCap) {
|
|
|
+ capChars = ["零", "壹", "贰", "叁", "肆", "伍", "陆", "柒", "捌", "玖"];
|
|
|
+ unitChars = ["" , "拾", "佰", "仟", "萬", "拾", "佰", "仟", "億", "拾", "佰", "仟", "萬"];
|
|
|
+ } else {
|
|
|
+ capChars = ["零", "一", "二", "三", "四", "五", "六", "七", "八", "九"];
|
|
|
+ unitChars = ["" , "十", "百", "千", "万", "十", "百", "千", "亿", "十", "百", "千", "万"];
|
|
|
}
|
|
|
- if (k % 4 === 2 && a[0].charAt(i + 2) !== 0 && a[0].charAt(i + 1) === 0) re = AA[0] + re;
|
|
|
- if (a[0].charAt(i) !== 0) re = AA[a[0].charAt(i)] + BB[k % 4] + re; k++;
|
|
|
- }
|
|
|
|
|
|
- if (a.length > 1) //加上小数部分(如果有小数部分)
|
|
|
- {
|
|
|
- re += BB[6];
|
|
|
- for (let i = 0; i < a[1].length; i++) re += AA[a[1].charAt(i)];
|
|
|
+ let numSplitArr = ("" + num).replace(/(^0*)/g, "").split(".");
|
|
|
+ if (numSplitArr[0] === "") numSplitArr[0] = "0";
|
|
|
+ let len = numSplitArr[0].length;
|
|
|
+ let intPartArr = [];
|
|
|
+ if (len <= 13) {
|
|
|
+ for (let idx = 0; idx < len; idx++) {
|
|
|
+ intPartArr.push(capChars[ parseInt(numSplitArr[0].charAt(idx) )] + unitChars[len - idx - 1]);
|
|
|
+ }
|
|
|
+ rst = intPartArr.join('');
|
|
|
+ rst = me.replaceAll(rst, capChars[0] + unitChars[3], capChars[0]); //零千 -> 零
|
|
|
+ rst = me.replaceAll(rst, capChars[0] + unitChars[2], capChars[0]); //零百 -> 零
|
|
|
+ rst = me.replaceAll(rst, capChars[0] + unitChars[1], capChars[0]); //零十 -> 零
|
|
|
+ //
|
|
|
+ rst = me.replaceAll(me.replaceAll(rst, "零零", "零"), "零零", "零");
|
|
|
+ rst = me.replaceAll(rst, capChars[0] + unitChars[8], unitChars[8]); //零亿 -> 亿
|
|
|
+ rst = me.replaceAll(rst, capChars[0] + unitChars[4], unitChars[4]); //零万 -> 万
|
|
|
+ //
|
|
|
+ rst = me.replaceAll(rst, unitChars[8] + unitChars[4], unitChars[8] + capChars[0]); //亿万 -> 亿零
|
|
|
+ if (num === 0) {
|
|
|
+ rst = "零";
|
|
|
+ } else if (rst.length > 1 && rst.charAt(rst.length - 1) === '零') {
|
|
|
+ rst = rst.slice(0, rst.length - 1);
|
|
|
+ }
|
|
|
+ //小数部分处理
|
|
|
+ if (numSplitArr.length > 1) {
|
|
|
+ len = numSplitArr[1].length;
|
|
|
+ if (isCurrency && len > 2) len = 2;
|
|
|
+ let fractionStr = [];
|
|
|
+ for (let idx = 0; idx < len; idx++) {
|
|
|
+ fractionStr.push(capChars[ parseInt(numSplitArr[1].charAt(idx))]+ (isCurrency?((idx === 0)?"角":"分"):""));
|
|
|
+ }
|
|
|
+ rst = rst + (isCurrency?"元":"点") + fractionStr.join("");
|
|
|
+ } else {
|
|
|
+ rst = rst + (isCurrency?"元整":"");
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ rst = "Number is too big!";
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ rst = "Number is wrong!";
|
|
|
}
|
|
|
- return re;
|
|
|
+ return rst;
|
|
|
},
|
|
|
convertStrToBoolean: function(str) {
|
|
|
let rst = false, me = this;
|