【问题标题】:Mule DWL 2.0 to convert number to wordMule DWL 2.0 将数字转换为单词
【发布时间】:2021-11-02 16:46:52
【问题描述】:

输入:123

输出:一百二十三

如何在dwl中将数字转换为单词

【问题讨论】:

    标签: mule transform mulesoft


    【解决方案1】:

    这个答案的灵感来自 Java solution 到手头的问题。然而,这还不够通用,仅适用于 3 位数字。不过,您绝对可以扩展这种方法。

    脚本

    %dw 2.0
    output json
    import * from dw::Runtime
    
    
    var units = [
        "",
        " one",
        " two",
        " three",
        " four",
        " five",
        " six",
        " seven",
        " eight",
        " nine"
    ]
     var twoDigits = [
        " ten",
        " eleven",
        " twelve",
        " thirteen",
        " fourteen",
        " fifteen",
        " sixteen",
        " seventeen",
        " eighteen",
        " nineteen"
     ]
    var tenMultiples = [
        "",
        "",
        " twenty",
        " thirty",
        " forty",
        " fifty",
        " sixty",
        " seventy",
        " eighty",
        " ninety"
    ]
      var placeValues = [
        " ",
        " thousand",
        " million",
        " billion",
        " trillion"
      ]
    
    var inputNum = 413
    
    
    fun wtonum(inp) = do {
        var word =""
        var index=0
        ---
    
    if((inp mod 1000) != 0)
        { a: (conv(inp mod 1000)  ) }
        else ""
    }
    
    fun conv(numInp) = do {
        var words = ""
        var calc = if((numInp mod 100) < 10) {a: units[numInp]} else if ((numInp mod 100) < 20) {a: twoDigits[(numInp mod 100) mod 10]} else {a: (tenMultiples[(numInp mod 100)/10] ++ units[(numInp mod 100) mod 10])}
        ---
        if(numInp/100 > 0.99) units[numInp/100] ++  " hundred" ++ calc.a else  calc.a
    }
    ---
    wtonum(inputNum)
    

    【讨论】:

    • 它不适用于低于 100 的值,它会出错
    • 已在消息中将其与解决方案一起指出。但是我已经对其进行了调整以解决 2 位和 3 位数字
    猜你喜欢
    • 1970-01-01
    • 2017-03-08
    • 2020-04-26
    • 2013-03-13
    • 2013-03-22
    相关资源
    最近更新 更多