数字转换人民币大写_____李刚《疯狂python》和《疯狂kotlin》都有这个例子,可是都没有写全,太懒了

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

}

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值