您好,欢迎来到宝玛科技网。
搜索
您的当前位置:首页python整数转换成英文表示

python整数转换成英文表示

来源:宝玛科技网

将非负整数num转换为其对应的英文表示

示例1:
输入:num=123
输出:one hundred twenty three

示例2:
输入:num=12345
输出:twelve thousand three hundred forty five

示例3:
输入:num=1234567
输出:one million two hundred thirty four thousand five hundred sixty seven

示例4:
输入:12345671
输出:one million two hundred thirty four million five hundred sixty seven thousand eight hundred ninety one

提示:

  • 0<=num<=2^31-1

实现代码

num=input("请输入一个整数:\n")
def less_Hundred(num):
    L1 = ["zero","one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten"]
    L2 = ["eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen", "seventeen", "eighteen", "nineteen",
          "twenty"]
    L3 = ["thirty", "forty", "fifty", "sixty", "seventy", "eighty", "ninety"]
    if int(num)<=10:
        return L1[int(num)]
    elif 10<int(num)<=20:
        return L2[int(num)%10-1]
    elif 20<int(num)<30:
        return L2[-1]+" "+L1[int(num)%10]
    elif 30<=int(num)<100:
        return L3[int(num)//10-3]+" "+L1[int(num)%10]
def less_thousand(num):
    if int(num)<100:
        return less_Hundred(num)
    if 100<=int(num)<1000:
        return less_Hundred(num[0])+" hundred "+less_Hundred(num[1:])
def less_million(num):
    s=""
    if int(num)<1000:
        s+=less_thousand(num)
    elif 1000<=int(num)<1000000:
        s+=less_thousand(num[:-3])+" thousand "
        if num[-3:]!="000":
            s+=less_thousand(num[-3:])
    elif 1000000000>int(num)>=1000000:
        s+=less_thousand(num[:-6])+" million "
        if num[-6:-3]!="000":
            s += less_thousand(num[-6:-3]) + " thousand "
        if num[-3:] != "000":
            s += less_thousand(num[-3:])
    elif int(num)>=1000000000:
        s+=less_thousand(num[:-9])+" billion "
        if num[-9:-6]!="000":
            s+=less_thousand(num[-9:-6])+" million "
        if num[-6:-3]!="000":
            s+=less_thousand(num[-6:-3]) + " thousand "
        if num[-3:]!="000":
            s+=less_thousand(num[-3:])
    return s


print(less_million(num))

因篇幅问题不能全部显示,请点此查看更多更全内容

Copyright © 2019- baomayou.com 版权所有 赣ICP备2024042794号-6

违法及侵权请联系:TEL:199 18 7713 E-MAIL:2724546146@qq.com

本站由北京市万商天勤律师事务所王兴未律师提供法律服务