- Joined
- Nov 19, 2020
- Posts
- 2,684
- Reaction
- 4,446
- Points
- 1,548
How to auto translate a number into word? For example, I have 1043 then I want to auto translate it to One Thousand and Fourty Three ?
def num_to_word(num):
ones = ['','One','Two','Three','Four','Five','Six','Seven','Eight','Nine']
tens = ['','Ten','Twenty','Thirty','Fourty','Fifty','Sixty','Seventy','Eighty','Ninety']
teens = ['Ten','Eleven','Twelve','Thirteen','Fourteen','Fifteen','Sixteen','Seventeen','Eighteen','Nineteen']
if num == 0:
return 'Zero'
# For numbers less than 10
elif num < 10:
return ones[num]
# For numbers less than 20
elif num < 20:
return teens[num-10]
# For numbers less than 100
elif num < 100:
return tens[num//10] + ' ' + ones[num%10]
# For numbers less than 1000
elif num < 1000:
return ones[num//100] + ' Hundred ' + num_to_word(num%100)
# For numbers less than 1000000
elif num < 1000000:
return num_to_word(num//1000) + ' Thousand ' + num_to_word(num%1000)
# For numbers less than 1000000000
elif num < 1000000000:
return num_to_word(num//1000000) + ' Million ' + num_to_word(num%1000000)
# For numbers less than 1000000000000
else:
return num_to_word(num//1000000000) + ' Billion ' + num_to_word(num%1000000000)
num_to_word(1043)
will return 'One Thousand Forty Three'
.