首页 > 代码库 > BPM实例分享——金额规则大写

BPM实例分享——金额规则大写

金额规则大写

 

在涉及金额的流程中经常会遇到需要大写金额数据与小写金额匹配,如何实现输入数字后自动转换呢?

初级用法

1.在默认表单基本属性javascript 中增加如下金额转换方法

/** 数字金额大写转换(可以处理整数,小数,负数) */

function chineseNumber(dValue) {

var maxDec = 2;

// 验证输入金额数值或数值字符串:

dValue = http://www.mamicode.com/dValue.toString().replace(/,/g,"");

dValue = http://www.mamicode.com/dValue.replace(/^0+/,""); // 金额数值转字符、移除逗号、移除前导零

if (dValue =http://www.mamicode.com/="") {

return "零元整";

} // (错误:金额为空!)

else if (isNaN(dValue)) {

return "错误:金额不是合法的数值!";

}

var minus = ""; // 负数的符号“-”的大写:“负”字。可自定义字符,如“(负)”。

var CN_SYMBOL = ""; // 币种名称(如“人民币”,默认空)

if (dValue.length > 1) {

if (dValue.indexOf(‘-‘) == 0) {

dValue = http://www.mamicode.com/dValue.replace("-", "");

minus = "负";

} // 处理负数符号“-”

if (dValue.indexOf(‘+‘) == 0) {

dValue = http://www.mamicode.com/dValue.replace("+", "");

} // 处理前导正数符号“+”(无实际意义)

}

// 变量定义:

var vInt = "";

var vDec = ""; // 字符串:金额的整数部分、小数部分

var resAIW; // 字符串:要输出的结果

var parts; // 数组(整数部分.小数部分),length=1时则仅为整数。

var digits, radices, bigRadices, decimals; // 数组:数字(0~9——零~玖);基(十进制记数系统中每个数字位的基是10——拾,佰,仟);大基(万,亿,兆,京,垓,杼,穰,沟,涧,正);辅币(元以下,角/分/厘/毫/丝)。

var zeroCount; // 零计数

var i, p, d; // 循环因子;前一位数字;当前位数字。

var quotient, modulus; // 整数部分计算用:商数、模数。

// 金额数值转换为字符,分割整数部分和小数部分:整数、小数分开来搞(小数部分有可能四舍五入后对整数部分有进位)。

var NoneDecLen = (typeof (maxDec) == "undefined" || maxDec == null || Number(maxDec) < 0 || Number(maxDec) > 5); // 是否未指定有效小数位(true/false)

parts = dValue.split(‘.‘); // 数组赋值:(整数部分.小数部分),Array的length=1则仅为整数。

if (parts.length > 1) {

vInt = parts[0];

vDec = parts[1]; // 变量赋值:金额的整数部分、小数部分

if (NoneDecLen) {

maxDec = vDec.length > 5 ? 5 : vDec.length;

} // 未指定有效小数位参数值时,自动取实际小数位长但不超5。

var rDec = Number("0." + vDec);

rDec *= Math.pow(10, maxDec);

rDec = Math.round(Math.abs(rDec));

rDec /= Math.pow(10, maxDec); // 小数四舍五入

var aIntDec = rDec.toString().split(‘.‘);

if (Number(aIntDec[0]) == 1) {

vInt = (Number(vInt) + 1).toString();

} // 小数部分四舍五入后有可能向整数部分的个位进位(值1)

if (aIntDec.length > 1) {

vDec = aIntDec[1];

} else {

vDec = "";

}

} else {

vInt = dValue;

vDec = "";

if (NoneDecLen) {

maxDec = 0;

}

}

if (vInt.length > 44) {

return "错误:金额值太大了!整数位长【" + vInt.length.toString() + "】超过了上限——44位/千正/10^43(注:1正=1万涧=1亿亿亿亿亿,10^40)!";

}

// 准备各字符数组 Prepare the characters corresponding to the digits:

digits = new Array("零", "壹", "贰", "叁", "肆", "伍", "陆", "柒", "捌", "玖"); // 零~玖

radices = new Array("", "拾", "佰", "仟"); // 拾,佰,仟

bigRadices = new Array("", "万", "亿", "兆", "京", "垓", "杼", "穰", "沟", "涧", "正"); // 万,亿,兆,京,垓,杼,穰,沟,涧,正

decimals = new Array("角", "分", "厘", "毫", "丝"); // 角/分/厘/毫/丝

resAIW = ""; // 开始处理

// 处理整数部分(如果有)

if (Number(vInt) > 0) {

zeroCount = 0;

for (i = 0; i < vInt.length; i++) {

p = vInt.length - i - 1;

d = vInt.substr(i, 1);

quotient = p / 4;

modulus = p % 4;

if (d == "0") {

zeroCount++;

} else {

if (zeroCount > 0) {

resAIW += digits[0];

}

zeroCount = 0;

resAIW += digits[Number(d)] + radices[modulus];

}

if (modulus == 0 && zeroCount < 4) {

resAIW += bigRadices[quotient];

}

}

resAIW += "元";

}

// 处理小数部分(如果有)

for (i = 0; i < vDec.length; i++) {

d = vDec.substr(i, 1);

if (d != "0") {

resAIW += digits[Number(d)] + decimals[i];

}

}

// 处理结果

if (resAIW == "") {

resAIW = "零" + "元";

} // 零元

if (vDec == "") {

resAIW += "整";

} // ...元整

resAIW = CN_SYMBOL + minus + resAIW; // 人民币/负......元角分/整

return resAIW;

}

 

2.在表单“小写金额”控件增加onchange事件

$.MvcSheetUI.MvcRuntime.setDataFieldControlValue($(this),‘jinedaxie‘,chineseNumber($("input[data-datafield=‘jinexiaoxie‘]").val()));

    金额(小写)数据项编码:jinexiaoxie

金额(大写)数据项编码:jinedaxie

 

高级用法

表单文本控件“ComputationRule”属性增加大写计算函数。

1.在\Portal\WFRes\_Scripts\MVCRuntime\Sheet.Computation.js文件中添加初级中的chineseNumber方法,在计算函数CompuationFun增加SUMCAPITAL定义。

{

        Name: "SUMCAPITAL",

        Accept: function (express) {

            return express.toLocaleLowerCase().indexOf("sumcapital(") == 0;

        },

        Compuator: function (obj, ctl, express, round) {

            var field = this.Field(express);

            express = this.Express(express);

            var val = 0;

            $("input[data-datafield=‘" + field + "‘],span[data-datafield=‘" + field + "‘]").each(function (obj, express, round) {

                var thisValue = http://www.mamicode.com/0;

                if (express) {

                    thisValue = http://www.mamicode.com/obj.executeCompute(this.id, round, express);

                }

                else {

                    if (this.tagName.toLocaleLowerCase() == "input" || this.tagName.toLocaleLowerCase() == "select")

                        thisValue = http://www.mamicode.com/this.value;

                    else

                        thisValue = http://www.mamicode.com/this.innerText;

                }

                if ($(this).attr("data-formatrule")) thisValue = http://www.mamicode.com/thisValue.replace(/,/g,"").replace(/$/g, "").replace(/¥/g, "");

                if (thisValue && !isNaN(thisValue)) {

                    val += parseFloat(thisValue);

                }

            }, [obj, express, round]);

            return chineseNumber(val);

        },

        Field: function (express) {

            var field = express.substring(express.indexOf("{") + 1);

            field = field.substring(0, field.indexOf("}"));

            return field;

        },

        Express: function (express) {

            if (express.indexOf(",") == -1) return "";

            var str = express.substring(express.indexOf(",") + 1);

            return str.substring(0, str.length - 1);

        },

        IsFunc: true

    },

2. 执行计算主方法判断中文计算,修改后如下

computator: function (id, round, express) {

        var v = this.executeCompute(id, round, express);

        try {

            eval(v); //判断是否中文数字

            // 结果四舍五入取小数位

            if (isFinite(v)) {

                v = Math.round(v * Math.pow(10, round)) / Math.pow(10, round);

                var target = $("#" + id);

                if (target.is("input,textarea")) {

                    var oldV = target.val().replace(/,/g, "").replace(/$/g, "").replace(/¥/g, "");

                    if (oldV != v) {

                        target.val(v);

                        target.trigger("change"); // target.trigger("change.MobileMaskText");

                        target.blur();

                    }

                }

                else if (target.is("div,span")) {

                    var oldV = target.html().replace(/,/g, "").replace(/$/g, "").replace(/¥/g, "");

                    if (oldV != v) {

                        target.html(v);

                        target.trigger("change"); //target.trigger("change.MobileMaskText");

                        target.blur();

                    }

                }

            } // end if(isFinite(v))

        }

        catch (e) {

            var target = $("#" + id);

            if (target.is("input,textarea")) {

                var oldV = target.val().replace(/,/g, "").replace(/$/g, "").replace(/¥/g, "");

                if (oldV != v) {

                    target.val(v);

                    target.trigger("change"); // target.trigger("change.MobileMaskText");

                    target.blur();

                }

            }

            else if (target.is("div,span")) {

                var oldV = target.html().replace(/,/g, "").replace(/$/g, "").replace(/¥/g, "");

                if (oldV != v) {

                    target.html(v);

                    target.trigger("change"); //target.trigger("change.MobileMaskText");

                    target.blur();

                }

            }

        }

    }

3.\Portal\WFRes\_Scripts\MVCRuntime\Sheet.js 文件计算结果值getResultValue定义修改如下

getResultValue: function (express) {

        if (express.indexOf("return") == -1) {

            try {

                return eval(express);}

            catch (e) {

            return express;}

        }

        else {

            return new Function(express).call(this);

        }

    },

 

4. \Portal\Admin\MvcDesigner\MvcDesigner.aspx表单中增加金额大写的按钮

<div style="margin-top: 10px;">

                <input type="button" id="btnSum" value="http://www.mamicode.com/SUM"

                    style="height: 28px; padding-left: 8px; padding-right: 8px;" />

                <input type="button" id="btnSumCapital" value="http://www.mamicode.com/SUMCAPITAL"

                    style="height: 28px; padding-left: 8px; padding-right: 8px;" />

                <input type="button" id="btnAvg" value="http://www.mamicode.com/AVG"

                    style="height: 28px; padding-left: 8px; padding-right: 8px;" />

                <input type="button" id="btnMax" value="http://www.mamicode.com/MAX"

                    style="height: 28px; padding-left: 8px; padding-right: 8px;" />

                <input type="button" id="btnMin" value="http://www.mamicode.com/MIN"

                    style="height: 28px; padding-left: 8px; padding-right: 8px;" />

                <input type="button" id="btnCount" value="http://www.mamicode.com/COUNT"

                    style="height: 28px; padding-left: 8px; padding-right: 8px;" />

            </div>

5. \Portal\Admin\MvcDesigner\MvcDesigner.aspx表单中增加金额大写的按钮事件

$("#btnSum,#btnSumCapital,#btnAvg,#btnCount,#btnMax,#btnMin").click(function () {

                var input = $("#txtComputationRule")[0];

                var text = $(this).val() + "()";

                var pos = getCaretPos(input);

                insertAtCaret(input, text);

                setCaretToPos(input, pos + text.length - 1);

 

    接下来控件的ComputationRule属性就可以使用大写计算公式了,是不是很酷,赶紧试一试吧。


 


BPM实例分享——金额规则大写