What's new

Number to words

Crypto Pitt

Forum Guru
Elite
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 ?
 
To automatically translate a number into words, you can use a function in Python. Here's an example code:

Python:
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)

You can then call this function and pass your number as an argument to get it translated into words. For example, num_to_word(1043) will return 'One Thousand Forty Three'.
 
Back
Top