🔒 Closed Number to words

Status
Not open for further replies.

Biscoff Frappe

Forum Expert
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'.
 
Status
Not open for further replies.

About this Thread

  • 1
    Replies
  • 414
    Views
  • 1
    Participants
Last reply from:
Unknown user

Trending Topics

Online now

Members online
378
Guests online
7,500
Total visitors
7,878

Forum statistics

Threads
2,339,156
Posts
29,308,099
Members
1,122,898
Latest member
barok97
Back
Top