Browse Source

加StringUtil

TonyKang 7 years ago
parent
commit
48b539af91

+ 29 - 25
modules/reports/rpt_component/helper/jpc_helper_common_output.js

@@ -2,7 +2,7 @@ let JV = require('../jpc_value_define');
 let JpcFieldHelper = require('./jpc_helper_field');
 
 let JpcCommonOutputHelper = {
-    createCommonOutputWithoutDecorate: function (node, value, controls) {
+    createCommonOutputWithoutDecorate: function (node, value) {
         let rst = {};
         //1. font/style/control
         rst[JV.PROP_FONT] = node[[JV.PROP_FONT]];
@@ -10,20 +10,11 @@ let JpcCommonOutputHelper = {
         rst[JV.PROP_STYLE] = node[[JV.PROP_STYLE]];
         //2. value
         rst[JV.PROP_VALUE] = value;
-        if (node[JV.PROP_FORMAT]) {
-            if (!(isNaN(parseFloat(rst[JV.PROP_VALUE])))) {
-                let dotIdx = node[JV.PROP_FORMAT].indexOf(".");
-                if (dotIdx >= 0) {
-                    rst[JV.PROP_VALUE] = parseFloat(rst[JV.PROP_VALUE]).toFixed(node[JV.PROP_FORMAT].length - dotIdx - 1);
-                } else {
-                    rst[JV.PROP_VALUE] = parseFloat(rst[JV.PROP_VALUE]).toFixed(0);
-                }
-            }
-        }
-        if (node[JV.PROP_PREFIX] && rst[JV.PROP_VALUE] != null) {
+        innerFormat(node[JV.PROP_FORMAT], rst);
+        if (node[JV.PROP_PREFIX] && rst[JV.PROP_VALUE] !== null) {
             rst[JV.PROP_VALUE] = node[JV.PROP_PREFIX] + rst[JV.PROP_VALUE];
         }
-        if (node[JV.PROP_SUFFIX] && rst[JV.PROP_VALUE] != null) {
+        if (node[JV.PROP_SUFFIX] && rst[JV.PROP_VALUE] !== null) {
             rst[JV.PROP_VALUE] = rst[JV.PROP_VALUE] + node[JV.PROP_SUFFIX];
         }
         return rst;
@@ -37,24 +28,37 @@ let JpcCommonOutputHelper = {
         //2. value
         rst[JV.PROP_VALUE] = value;
         JpcFieldHelper.decorateValue(rst, controls);
-        if (node[JV.PROP_FORMAT]) {
-            if (!(isNaN(parseFloat(rst[JV.PROP_VALUE])))) {
-                let dotIdx = node[JV.PROP_FORMAT].indexOf(".");
-                if (dotIdx >= 0) {
-                    rst[JV.PROP_VALUE] = parseFloat(rst[JV.PROP_VALUE]).toFixed(node[JV.PROP_FORMAT].length - dotIdx - 1);
-                } else {
-                    rst[JV.PROP_VALUE] = parseFloat(rst[JV.PROP_VALUE]).toFixed(0);
-                }
-            }
-        }
-        if (node[JV.PROP_PREFIX] && rst[JV.PROP_VALUE] != null && rst[JV.PROP_VALUE] != "") {
+        innerFormat(node[JV.PROP_FORMAT], rst);
+        if (node[JV.PROP_PREFIX] && rst[JV.PROP_VALUE] !== null && rst[JV.PROP_VALUE] !== "") {
             rst[JV.PROP_VALUE] = node[JV.PROP_PREFIX] + rst[JV.PROP_VALUE];
         }
-        if (node[JV.PROP_SUFFIX] && rst[JV.PROP_VALUE] != null && rst[JV.PROP_VALUE] != "") {
+        if (node[JV.PROP_SUFFIX] && rst[JV.PROP_VALUE] !== null && rst[JV.PROP_VALUE] !== "") {
             rst[JV.PROP_VALUE] = rst[JV.PROP_VALUE] + node[JV.PROP_SUFFIX];
         }
         return rst;
     }
+};
+
+function innerFormat(formatStr, rst) {
+    if (formatStr) {
+        if (!(isNaN(parseFloat(rst[JV.PROP_VALUE])))) {
+            let dotIdx = formatStr.indexOf(".");
+            if (dotIdx >= 0) {
+                rst[JV.PROP_VALUE] = parseFloat(rst[JV.PROP_VALUE]).toFixed(formatStr.length - dotIdx - 1);
+            } else {
+                rst[JV.PROP_VALUE] = parseFloat(rst[JV.PROP_VALUE]).toFixed(0);
+            }
+            let commaIdx = formatStr.indexOf(",");
+            if (commaIdx >= 0) {
+                rst[JV.PROP_VALUE] = comdify(rst[JV.PROP_VALUE].toString());
+            }
+        }
+    }
+}
+
+function comdify(numStr){
+    let re = /\d{1,3}(?=(\d{3})+$)/g;
+    return numStr.replace(/^(\d+)((\.\d+)?)$/,function(s,s1,s2){return s1.replace(re,"$&,")+s2;});
 }
 
 module.exports = JpcCommonOutputHelper;

+ 1 - 0
modules/reports/rpt_component/jpc_bill_tab.js

@@ -1,4 +1,5 @@
 let JV = require('./jpc_value_define');
+let JE = require('./jpc_rte');
 let JpcFieldHelper = require('./helper/jpc_helper_field');
 let JpcBandHelper = require('./helper/jpc_helper_band');
 let JpcCommonHelper = require('./helper/jpc_helper_common');

+ 2 - 1
modules/reports/rpt_component/jpc_rte.js

@@ -1,9 +1,10 @@
 /**
  * Created by Tony on 2016/12/28.
  */
-
+let strUtil = require('../../../public/stringUtil');
 let JV = require('./jpc_value_define');
 let JE = {
+    $STR_UTIL: strUtil,
     F: function(fID, $CURRENT_RPT) {
         let rst = null;
         if ($CURRENT_RPT && ($CURRENT_RPT.fields[JV.NODE_DETAIL_FIELDS][JV.PROP_ID + "_" + fID])) {

+ 13 - 5
public/stringUtil.js

@@ -149,6 +149,10 @@ module.exports = {
         let regExp = new RegExp(FindText, "gm");
         return targetStr.replace(regExp, RepText);
     },
+    comdify: function(numStr){
+        let re = /\d{1,3}(?=(\d{3})+$)/g;
+        return numStr.replace(/^(\d+)((\.\d+)?)$/,function(s,s1,s2){return s1.replace(re,"$&,")+s2;});
+    },
     convertToCaptionNum: function(num, isCurrency, isTraditionalCap) {
         let me = this, rst = "";
         if (/^\d*(\.\d*)?$/.test(num)) {
@@ -187,12 +191,16 @@ module.exports = {
                 //小数部分处理
                 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)?"角":"分"):""));
+                    if (parseInt(numSplitArr[1]) === 0) {
+                        rst = rst + (isCurrency?"元整":"");
+                    } else {
+                        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("");
                     }
-                    rst = rst + (isCurrency?"元":"点") + fractionStr.join("");
                 } else {
                     rst = rst + (isCurrency?"元整":"");
                 }

+ 43 - 5
test/demo/stringTest.js

@@ -55,9 +55,47 @@ var strUtil = require('../../public/stringUtil');
 //     t.end();
 // })
 
-test('test typeof', function(t){
-    t.equal(typeof(true), "boolean");
-    t.equal((typeof("abc")).toUpperCase(), "STRING");
-    t.equal(typeof(1), "number");
+// test('test typeof', function(t){
+//     t.equal(typeof(true), "boolean");
+//     t.equal((typeof("abc")).toUpperCase(), "STRING");
+//     t.equal(typeof(1), "number");
+//     t.end();
+// })
+
+test('test 千分位', function(t){
+    let num = 123.01;
+    t.equal( strUtil.comdify(num.toString()), "123.01");
+    num = 123456.01;
+    t.equal( strUtil.comdify(num.toString()), "123,456.01");
+    num = 1234567.01;
+    t.equal( strUtil.comdify(num.toString()), "1,234,567.01");
+    num = 1234567;
+    t.equal( strUtil.comdify(String(num)), "1,234,567");
+    t.end();
+})
+
+
+test('test number to Chinese', function(t){
+    t.equal(strUtil.convertToCaptionNum(0, false, false), '零');
+    t.equal(strUtil.convertToCaptionNum(0, true, false), '零元整');
+    t.equal(strUtil.convertToCaptionNum(0.68, true, false), '零元六角八分');
+    t.equal(strUtil.convertToCaptionNum(0.68, true, true), '零元陆角捌分');
+    t.equal(strUtil.convertToCaptionNum(10, false, false), '一十');
+    t.equal(strUtil.convertToCaptionNum(10, false, true), '壹拾');
+    t.equal(strUtil.convertToCaptionNum(11, false, false), '一十一');
+    t.equal(strUtil.convertToCaptionNum(11, false, true), '壹拾壹');
+    t.equal(strUtil.convertToCaptionNum(12, false, false), '一十二');
+    t.equal(strUtil.convertToCaptionNum(12, false, true), '壹拾贰');
+    t.equal(strUtil.convertToCaptionNum(21, false, false), '二十一');
+    t.equal(strUtil.convertToCaptionNum(21, false, true), '贰拾壹');
+
+    t.equal(strUtil.convertToCaptionNum(0.123456789, false, false), '零点一二三四五六七八九');
+    t.equal(strUtil.convertToCaptionNum(10.123456789, false, false), '一十点一二三四五六七八九');
+
+    t.equal(strUtil.convertToCaptionNum(123456789.15, false, false), '一亿二千三百四十五万六千七百八十九点一五');
+    t.equal(strUtil.convertToCaptionNum(123456789.15, false, true), '壹億贰仟叁佰肆拾伍萬陆仟柒佰捌拾玖点壹伍');
+    t.equal(strUtil.convertToCaptionNum(123456789.15, true, false), '一亿二千三百四十五万六千七百八十九元一角五分');
+    t.equal(strUtil.convertToCaptionNum(123456789.15, true, true), '壹億贰仟叁佰肆拾伍萬陆仟柒佰捌拾玖元壹角伍分');
+
     t.end();
-})
+})