Python重写:
numStr = input("请输入待转换数字:")
rmbStr = "零壹贰叁肆伍陆柒捌玖"
unitStr = "分角元拾佰仟万拾佰仟亿拾佰仟"
result = ""
numStr2 = str(int(float(numStr) * 100))
i = 0
for i in range(len(numStr2)):
#print(i)
unit1 = unitStr[len(numStr2) - 1 - i]
num = rmbStr[int(numStr2[i])]
result = result + num + unit1
result = result + "整"
print(result)
for m in range(len(result)):
result = result.replace("零拾", "零")
result = result.replace("零佰", "零")
result = result.replace("零仟", "零")
result = result.replace("零万", "万")
result = result.replace("零亿", "亿")
result = result.replace("零角", "零")
result = result.replace("零分", "零")
result = result.replace("零零", "零")
result = result.replace("零元", "元")
result = result.replace("元零", "元")
result = result.replace("角零", "角")
result = result.replace("亿万", "亿")
result = result.replace("分整", "分")
m = m+1
print(result)
Kotlin重写:
fun main(args: Array<String>) {
println(toHanStr("236711125"))
println(toHanStr("1345023566009.11"))
println(toHanStr("6009.1"))
println(toHanStr("610000100109"))
println(toHanStr("10000000009"))
}
fun toHanStr(numStr: String): String {
var hanArr = arrayOf("零", "壹", "贰", "叁", "肆", "伍", "陆", "柒", "捌", "玖")
var unitArr = arrayOf("分", "角", "元", "拾", "佰", "仟", "万", "拾", "佰", "仟", "亿", "拾", "佰", "仟", "万")
var result: String = ""
var numStr2 = (numStr.toDouble() * 100).toLong().toString()
//println(numStr2)
for (i in 0 until numStr2.length) {
var num = numStr2[i].toInt() - 48
//println(num)
result =result + hanArr[num] + unitArr[numStr2.length - 1 - i]
//println(result)
}
result = result + "整"
for (i in 0 until numStr2.length) {
result = result.replace("零拾", "零")
result = result.replace("零佰", "零")
result = result.replace("零仟", "零")
result = result.replace("零万", "万")
result = result.replace("零亿", "亿")
result = result.replace("零角", "零")
result = result.replace("零分", "零")
result = result.replace("零零", "零")
result = result.replace("零元", "元")
result = result.replace("元零", "元")
result = result.replace("角零", "角")
result = result.replace("亿万", "亿")
result = result.replace("分整", "分")
}
return result
}