qdrelang
发表于 2020-2-19 22:16:17
def engnum(num):
english=['','One','Two','Three',\
'Four','Five','Six','Seven','Eight',\
'Nine','Ten','Eleven','Twelve','Thirteen',\
'Fourteen','Fifteen','Sixteen','Seventeen',\
'Eighteen','Nineteen','Twenty','Thirty','Forty','Fifty',\
'Sixty','Seventy','Eighty','Ninety','Hundred',\
'Thousand','Million','Billion']
english1=[' ',' ','Twenty','Thirty','Forty','Fifty',\
'Sixty','Seventy','Eighty','Ninety']
if num < 21 :
return english
else:
if num<100:
return english1+''+english
else:
if num<1000:
returnenglish+''+'Hundred'+''+engnum(num % 100)
else:
if num<1000000:
return engnum(num//1000)+''+'Thousand'+''+engnum(num % 1000)
else:
if num<1000000000:
return engnum(num//1000000)+''+'Million'+''+engnum(num % 1000000)
else:
return engnum(num//1000000000)+''+'Billion'+''+engnum(num % 1000000000)
num = int(input('请输入一个数字:'))
print('%d的英文写法是:' %num ,engnum(num))
rsj0315
发表于 2020-3-30 13:25:08
最后的魁拔 发表于 2020-2-14 18:39
有个好办法,爬取有道翻译
抓紧爬起来,哈哈
776667
发表于 2021-5-13 10:43:45
本帖最后由 776667 于 2021-5-13 10:45 编辑
def fun332(x):
if not x:
return 'Zero'
x = str(x)
within_twenty = ['Zero','One','Two','Three','Four','Five','Six','Seven','Eight','Nine','Ten',
'Eleven','Twelve','Thirteen','Fourteen','Fifteen','Sixteen','Seventeen','Eighteen','Nineteen']
tens = ['Twenty','Thirty','Forty','Fifty','Sixty','Seventy','Eighty','Ninety']
def prefix(n,unit='',space=' '):
n = str(int(n))
if n == '0':
return ''
if len(n) == 1:
return '%s%s%s'%(space,within_twenty,unit)
if len(n) == 2:
if int(n) < 20:
return '%s%s%s'%(space,within_twenty,unit)
elif int(n) >= 20 and not int(n)%10:
return '%s%s%s'%(space,tens)-2],unit)
else:
return '%s%s %s%s'%(space,tens)-2],within_twenty)],unit)
if len(n) == 3:
if not int(n)%100:
return '%s%s Hundred%s'%(space,within_twenty)],unit)
elif int(n) >= 20 and not int(n)%10:
return '%s%s Hundred %s%s'%(space,within_twenty)],tens)-2],unit)
else:
return '%s%s Hundred %s %s%s'%(space,within_twenty)],tens)-2],within_twenty)],unit)
if len(x) <= 3:
return prefix(x)
elif len(x) <= 6:
return '%s%s'%(prefix(x[:-3],' Thousand',space=''),prefix(x[-3:]))
elif len(x) <= 9:
return '%s%s%s'%(prefix(x[:-6],' Million',space=''),prefix(x[-6:-3],' Thousand'),prefix(x[-3:]))
else:
return '%s%s%s%s'%(prefix(x[:-9],' Billion',space=''),prefix(x[-9:-6],' Million'),prefix(x[-6:-3],' Thousand'),prefix(x[-3:]))