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()
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')