🔒 Closed Number to words

Status
Not open for further replies.
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
  • 377
    Views
  • 1
    Participants
Last reply from:
Unknown user

Trending Topics

Online now

Members online
966
Guests online
1,155
Total visitors
2,121

Forum statistics

Threads
2,273,892
Posts
28,952,250
Members
1,234,982
Latest member
guatno
Back
Top