编辑代码

def convertToUppercase(String sost) {
    // 处理输入,确保是标准数字格式
    def amount
    try {
        amount = sost.contains('.') ? Double.parseDouble(sost) : Integer.parseInt(sost)
    } catch (NumberFormatException e) {
        println "输入格式错误,请输入有效的数字"
        return
    }

    // 检查数值范围
    if (amount < 0 || amount > 9999999999999.99) {
        println "输入数值超出范围"
        return
    }

    // 数字大写映射
    def numMap = ['零', '壹', '贰', '叁', '肆', '伍', '陆', '柒', '捌', '玖']
    def unitMap = [
        '元': 1,
        '万': 5,
        '亿': 9,
        '兆': 13
    ]
    def decimalUnit = ['角', '分']

    // 分离整数和小数部分
    def integerPart = Math.floor(amount).toLong()
    def decimalPart = Math.round((amount - integerPart) * 100)

    // 转换整数部分
    def result = ''
    def processed = false

    unitMap.each { unit, position ->
        def currentPart = (integerPart / Math.pow(10, position - 1)) % 10000
        if (currentPart >= 1) {
            def partStr = ''
            def thousands = (currentPart / 1000).toInteger()
            def hundreds = ((currentPart % 1000) / 100).toInteger()
            def tens = ((currentPart % 100) / 10).toInteger()
            def ones = (currentPart % 10).toInteger()

            if (thousands > 0) {
                partStr += "${numMap[thousands]}仟"
            }
            if (hundreds > 0) {
                partStr += "${numMap[hundreds]}佰"
            } else if (thousands > 0 && (tens > 0 || ones > 0)) {
                partStr += "零"
            }
            if (tens > 0) {
                if (tens == 1 && thousands == 0 && hundreds == 0) {
                    partStr += "拾"
                } else {
                    partStr += "${numMap[tens]}拾"
                }
            } else if (hundreds > 0 && ones > 0) {
                partStr += "零"
            }
            if (ones > 0) {
                partStr += numMap[ones]
            }

            partStr += unit
            result = partStr + result
            processed = true
        } else if (processed) {
            result = "零" + result
        }
    }

    // 处理小数部分
    if (decimalPart > 0) {
        def jiao = (decimalPart / 10).toInteger()
        def fen = (decimalPart % 10).toInteger() // 修复:明确转换为Integer
        if (jiao > 0) {
            result += "${numMap[jiao]}${decimalUnit[0]}"
        } else if (result) {
            result += "零"
        }
        if (fen > 0) {
            result += "${numMap[fen]}${decimalUnit[1]}"
        }
    } else {
        result += "整"
    }

    // 清理多余的零
    result = result.replaceAll('零+', '零')
    result = result.replaceAll('零([万亿兆])', '$1')
    result = result.replaceAll('^零+', '')
    result = result.replaceAll('零元', '元')

    return result
}

// 测试用例
println convertToUppercase('52.00')  // 输出:伍拾贰元整
println convertToUppercase('52')     // 输出:伍拾贰元整
println convertToUppercase('12.23')  // 输出:壹拾贰元贰角叁分
println convertToUppercase('0.23')   // 输出:贰角叁分
println convertToUppercase('1001.01') // 输出:壹仟零壹元零壹分