【小白教程】如何实现阿拉伯数字转换成英文大写?

分享 明道云APaaS  收藏
2 / 785

需求描述

外贸企业,或者涉外单位的英文合同或者形式发票,总是需要显示英文的数字大写。 如下图:

image.png

这是我的 Proforma Invoice 形式发票表的一个截图,里面存放这总金额数字字段。
另外一个英文大写字段,是文本框字段,用来存放转换后的英文大写。

怎么用明道云的工作流 + 代码块来实现呢?

工作流如下图:

image.png

配置表触发方式为 新增或者更新总金额字段触发就可以。

主要是代码块设置如下图:
image.png

其中 num 动态值就是你的需要转换成英文大写的阿拉伯数字总金额。第一次测试的时候,可以给 num 一个具体的值,而不是动态的字段内容,例如 1231231 这样。

具体的代码如下:

//var num='128040.00';
var endresult = englishmoney(num);
function englishmoney(num) {
    var i;
    var hundreds;
    var tenth;
    var one;
    var thousand;
    var million;
    var billion;
    var numbers;
    var s
    var result; 
    var str= '000000000000000' + num.toString();
    numbers = 'one       two       three     four      five      ' + 'six       seven     eight     nine      ten       ' + 'eleven    twelve    thirteen  fourteen  fifteen   ' + 'sixteen   seventeen eighteen  nineteen  ' + 'twenty    thirty    forty     fifty     ' + 'sixty     seventy   eighty    ninety    ';
    String.prototype.Trim = function() {
        return this.replace(/^\s+|\s+$/g, "");
    }
    String.prototype.Ltrim = function() {
        return this.replace(/^\s+/g, "");
    }
    String.prototype.Rtrim = function() {
        return this.replace(/\s+$/g, "");
    }
    s = str.substring(str.length - 15, str.length);
    billion = parseInt(s.substring(0, 3)); //将12位整数分成4段:十亿、百万、千、百十个
    million = parseInt(s.substring(3, 6));
    thousand = parseInt(s.substring(6, 9));
    result = '';
    i = 0;

    while (i <= 3) {
        hundreds = parseInt(s.substring(i * 3, i * 3 + 1)); //百位0-9
        tenth = parseInt(s.substring(i * 3 + 1, i * 3 + 2));
        if (tenth == 1) {
            one = 10 + parseInt(s.substring(i * 3 + 2, i * 3 + 3));
        } else {
            one = 0 + parseInt(s.substring(i * 3 + 2, i * 3 + 3));
        }
        // one=(CASE @tenth WHEN 1 THEN 10 ELSE 0 END)+CAST(SUBSTRING(@s,@i*3+3,1) AS int)--个位0-19
        if (tenth <= 1) {
            tenth = 0;
        }

        // tenth=(CASE WHEN @tenth<=1 THEN 0 ELSE @tenth END)--十位0、2-9f
        if ((i == 1 && parseInt(billion) > 0 && (parseInt(million) > 0 || parseInt(thousand) > 0 || parseInt(hundreds) > 0)) || (i == 2 && (parseInt(billion) > 0 || parseInt(million) > 0) && (parseInt(thousand) > 0 || parseInt(hundreds) > 0)) || (i == 3 && (parseInt(billion) > 0 || parseInt(million) > 0 || parseInt(thousand) > 0) && (parseInt(hundreds) > 0))) {
            result = result + ' ';
        } //--百位不是0则每段之间加连接符,
        if (i == 3 && (billion > 0 || million > 0 || thousand > 0) && (hundreds == 0 && (tenth > 0 || one > 0))) {
            result = result + ' and ';
        } //--百位是0则加连接符AND
        if (hundreds > 0) {
            result = result + numbers.substring(hundreds * 10 - 10, hundreds * 10).Rtrim() + ' hundred';
        }

        if (tenth >= 2 && tenth <= 9) {
            if (hundreds > 0) {
                result = result + ' and ';
            }
            result = result + numbers.substring(tenth * 10 + 170, tenth * 10 + 180).Rtrim();
        }

        if (one >= 1 && one <= 19) {
            if (tenth > 0) {
                result = result + '-';
            } else {
                if (hundreds > 0) {
                    result = result + ' and ';
                }
            }
            result = result + numbers.substring(one * 10 - 10, one * 10).Rtrim();
        }
        //alert(result);
        // i=i+1;
        // } 
        if (i == 0 && parseInt(billion) > 0) {
            result = result + ' billion';
        }
        if (i == 1 && parseInt(million) > 0) {
            result = result + ' million';
        }
        if (i == 2 && parseInt(thousand) > 0) {
            result = result + ' thousand';
        }
        i = i + 1;
    }
    if (s.substring(13, 15) != '00') {
        result = result + ' point ';
        if (s.substring(13, 14) == '0') {
            result = result + 'zero';
        } else {
            result = result + numbers.substring(parseInt(s.substring(13, 14)) * 10 - 10, parseInt(s.substring(13, 14)) * 10).Rtrim();
        }
        if (s.substring(14, 15) != '0') {
            result = result + ' ' + numbers.substring(parseInt(s.substring(14, 15)) * 10 - 10, parseInt(s.substring(14, 15)) * 10).Rtrim();
        }
    }
    return result;
}
output = {    result: endresult.toUpperCase()}

更新表单里面的英文大写文本框

image.png

特此感谢:衣锦夜行、Anne 熊_敏 的大力支持和耐心讲解。