Numbers

Number to word converter

Project Description

This project is a part of a Django web application that allows users to input a numeric value and receive the corresponding word form of that number. The application provides a simple web interface where users can interact with the converter.

The project is motivated by the practical application of recursion and memoization concepts.

Note

There is no imported module used for this work.

Overview: A brief summary

This is the unique dictionary as the base for all word derivatives

unique_numbers
{'0': 'Zero', '1': 'One', '2': 'Two', '3': 'Three', '4': 'Four', '5': 'Five', '6': 'Six', '7': 'Seven', '8': 'Eight',
'9': 'Nine', '10': 'Ten', '11': 'Eleven', '12': 'Twelve', '13': 'Thirteen', '14': 'Forteen', '15': 'Fifteen',
'16': 'Sixteen', '17': 'Seventeen', '18': 'Eighteen', '19': 'Nineteen', '20': 'Twenty', '30': 'Thirty',
'40': 'Forty', '50': 'Fifty', '60': 'Sixty', '70': 'Seventy', '80': 'Eighty', '90': 'Ninety'
}

The recursive function that processes with the dictionary above

numtoword.convert(figure=str) str:

Return a string in words of the figure entered.

Parameters:

figure – number entered by user

Raises:

OverflowError – If the figure exceed the stipulated limit.

Returns:

The word form of the number entered.

Return type:

str

Step by Step approach

Warning

To explain the code, I will split it into sub-functions as each sub is independent of the other. So the many sub-functions is for explanation purposes only and do not represent the one function code base

Step 1 Test the user input is convertable to integer: Though restrictions have been placed on the user input form, it is still validated.

0 - 20: The number entered by a user is first checked to confirm if it is less than or equal to 20. If it is, it takes the word form from the dictionary.

First sub-function
def sub_zero_to_twenty(figure):
    if int(figure) <= 20:
        return unique_numbers[figure]

21 - 99: If the user’s input is between 20 and 100 exclusive, it will first check if the last digit is zero and return it’s word form from the dictionary. If it is not zero, then it will take the first digit, append zero to it and fetch the word form from the dictionary. Then, it will take the second digit and return the word form from same dictionary. Finally, it will concatenate the two results to form the complete word.

Second sub-function
def sub_twenty_one_to_ninety_nine(figure):
    if int(figure) > 20 and int(figure) < 100:
        # break into two
        if figure[1] == '0':
            result = unique_numbers[figure]
        else:
            result = f"{unique_numbers[figure[0]+'0']} {unique_numbers[figure[1]]}"
    return result

100 - 999:

Third sub-function
def sub_hundred_to_nine_nine_nine(figure):

    if int(figure) >= 100 and int(figure) < 1000:
        # tear the number
        first_number = convert(figure[0])
        last_number = figure[1:]
        # zeros = dataset['00']
        if last_number == 2*'0':
            result = f'{first_number} Hundred'
        else:
            last_number = str(int(last_number))
            last_number = convert(last_number)
            result =  f'{first_number} Hundred and {last_number}'