Python program for Converting digit/number to words

Converting digit/number to words

The conversion of numbers in words is just a conversion of numeric values to the English format of reading numbers. This code supports the conversion of numbers from 0 – 9999 in English format. Digits have different places when read from its ones place to above. Different places for numbers are:-

  • Single digits:- Ones
  • Two Digits:-Tens
  • Three Digits:- Hundreds
  • Four Digits:- Thousands

Etc. as in this program we will be converting only up-to 4-Digits

Python Program for converting numbers/digits in words

Algorithm

  • Step 1:- Start.
  • Step 2:- Taking input as a string from the user.
  • step 3:- Check the length of the input.
  • step 4:- if the length is zero print ’empty’ and if the length is greater than 4 print ‘give a string of specific length’
  • Step 5:- if length id between 1 – 4, Create arrays for different values.
  • Step 6:- Checking the length of the string.
  • Step 7:- According to the place of the digit, we will show the output.
  • Step 8:- End.


Python Program

def numToWords(num):
    length_of_string = len(num);  
    if (length_of_string == 0): 
        print("String is Empty"); 
        return; 
    if (length_of_string > 4): 
        print("Please enter the string with supported length"); 
        return;  
    ones_digits = ["zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine"]; 
    tens_digits = ["", "ten", "eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen", "seventeen", "eighteen","nineteen"]; 
    multiple_of_ten = ["", "", "twenty", "thirty", "forty", "fifty", "sixty", "seventy", "eighty", "ninety"]; 
    power_of_ten = ["hundred", "thousand"];   
    print(num, ":", end = " ");
    if (length_of_string == 1):  
        print(ones_digits[ord(num[0]) - '0']); 
        return;
    x = 0; 
    while (x < len(num)): if (length_of_string >= 3): 
            if (ord(num[x]) - 48 != 0): 
                print(ones_digits[ord(num[x]) - 48], end = " "); 
                print(power_of_ten[length_of_string - 3], end = " ");  
            length_of_string -= 1; 
        else: 
            if (ord(num[x]) - 48 == 1):  
                sum = (ord(num[x]) - 48 + ord(num[x]) - 48); 
                print(tens_digits[sum]); 
                return;
            elif (ord(num[x]) - 48 == 2 and ord(num[x + 1]) - 48 == 0): 
                print("twenty"); 
                return; 
            else: 
                i = ord(num[x]) - 48; 
                if(i > 0): 
                    print(multiple_of_ten[i], end = " "); 
                else: 
                    print("", end = ""); 
                x += 1; 
                if(ord(num[x]) - 48 != 0): 
                    print(ones_digits[ord(num[x]) - 48]); 
        x += 1; 
numToWords("1121")
Output:
1121 : one thousand one hundred twenty one

2 comments on “Python program for Converting digit/number to words”


  • Bhoma

    one = [ “”, “one “, “two “, “three “, “four “,
    “five “, “six “, “seven “, “eight “,
    “nine “, “ten “, “eleven “, “twelve “,
    “thirteen “, “fourteen “, “fifteen “,
    “sixteen “, “seventeen “, “eighteen “,
    “nineteen “];

    ten = [ “”, “”, “twenty “, “thirty “, “forty “,
    “fifty “, “sixty “, “seventy “, “eighty “,
    “ninety “];

    # This code only the range of 0 to 100
    def numToWords(n, s):

    str = “”;
    if (n > 19):
    str += ten[n // 10] + one[n % 10];
    else:
    str += one[n];
    if (n):
    str += s;

    return str;

    # This is for 20 to almost infinity

    # Function to print a given number in words
    def convertToWords(n):
    out = “”;
    out += numToWords((n // 10000000), “crore “);
    out += numToWords(((n // 100000) % 100),”lakh “);
    out += numToWords(((n // 1000) % 100),”thousand “);
    out += numToWords(((n // 100) % 10),”hundred “);

    if (n > 100 and n % 100):
    out += “and “;
    out += numToWords((n % 100), “”);

    return out;

    print(convertToWords(8000));