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 si
·
将非负整数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:
输入:1234567891
输出: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))
更多推荐
已为社区贡献1条内容
所有评论(0)