Number to word converter - A cool python project

 

cool python project
icon is taken from pngpath.com

Importance of the "Number to word converter -  A cool python project"

Number to word converter is needed in the cheque, especially in the production cheque. In the production sector and bank sector, both numeric digit and written word numbers are equally important to maintain the formality.

cool python project

"standard dictionary numbers" for "Number to word converter -  A python project for practice"

Digits in value with its name (one to billion)

Digits/Value In Word Name

1 one

10 ten

100 hundred

1,000 thousand

10,000 ten thousand

100,000 hundred thousand

1,000,000 million

10,000,000 ten million

100,000,000 hundred million

1,000,000,000 billion


Digits in number with places (one to billion)

1  0  0  0  0  0  0  0  0  0

|  |  |  |  |  |  |  |  |  |____ Ones'

|  |  |  |  |  |  |  |  |__ ____ Tens'

|  |  |  |  |  |  |  |__ __ ____ Hundreds'

|  |  |  |  |  |  |__ __ __ ____ Thousands'

|  |  |  |  |  |__ __ __ __ ____ Tens Thousands'

|  |  |  |  |__ __ __ __ __ ____ Hundred Thousands'

|  |  |  |__ __ __ __ __ __ ____ One Millions'

|  |  |__ __ __ __ __ __ __ ____ Ten Millions'

|  |__ __ __ __ __ __ __ __ ____ Hundred Millions'

|__ __ __ __ __ __ __ __ __ ____ One Billions'



Algorithm for "Number to word converter -  A python project for practice"

Large number name rule

-> initialize the dictionary for large number names. For this post, the large number of names is limited to zero to a nonillion.

Number rules

->Zero Rule

If the input value is zero, then the output will be zero.

NumberIntoWord+='zero'

->3-Digit Rule

3-Digit words are: "thousand", "million", "billion", "trillion", "quadrillion", "quintillion", "sextillion", "septillion", "octillion", "nonillion". (From Right to the Left )

The 3-digit words will be appeared in the text according to the hierarchy. (Right to left method)

NumberIntoWord+=3-DigitWords[i]

->Hundred Rule

If the input three-digit number is divisible by 1 hundred, then the text 'hundred' will be appended.

NumberIntoWord+='hundred'

Else, 'hundred and' will be appended to the word.

NumberIntoWord+='hundred and'


->Tens Rule

If a three-digit group's tens section is two or higher, the corresponding '-ty' word (twenty, thirty, forty, fifty, sixty, etc.) is appended to the output final text (NumberIntoWord), followed by the name of the third digit (unless the third digit is a zero).

NumberIntoWord+='ty'

There is no text added if the tens and units are both zero. 

NumberIntoWord+=''

->Recombination or Comma Rule

                For the recombining of the final word as an output, the three-digit group is followed by a comma and a large number name.

                If the three-digit group (million, billion, etc) does not have any hundred, then the final comma in the NumberIntoWord is replaced by a 'and'. (ex: two million and two)

NumberIntoWord+=','

NumberIntoWord.replace([:]word, ',', 'and')

->Negative Rule

Check the first of the input number and if the sign'-' (negative), then the text 'negative' will be appended.

NumberIntoWord+='negative'

Python code for "Number to word converter -  A python project for practice"

OneDigitWords = {
'0': ["zero"],
'1': ["one"],
'2': ["two", "twen"],
'3': ["three", "thir"],
'4': ["four", "for"],
'5': ["five", "fif"],
'6': ["six"],
'7': ["seven"],
'8': ["eight"],
'9': ["nine"],
}
TwoDigitWords = ["ten", "eleven", "twelve"]
Hundred = "hundred"
LargeMathWords = ["thousand", "million", "billion", "trillion", "quadrillion", "quintillion", "sextillion", "septillion", "octillion", "nonillion"]

def IsNumberNegative(NumberInToWord, InputNumber):
if InputNumber.startswith('-'):
NumberInToWord.append("(Negative)")
InputNumber = InputNumber[1:]
return NumberInToWord, InputNumber

def NumberInToWordConverter(InputNumber):
NumberInToWord = []

NumberInToWord, InputNumber=IsNumberNegative(NumberInToWord, InputNumber)

if len(InputNumber) % 3 != 0 and len(InputNumber) > 3:
InputNumber = InputNumber.zfill(3 * (((len(InputNumber) - 1) // 3) + 1))

SumOfList = [InputNumber[i:i + 3] for i in range(0, len(InputNumber), 3)]
skip = False

for i, number in enumerate(SumOfList):
if number != '000': skip = False

for _ in range(len(number)):
number = number.lstrip('0')
if len(number) == 1:
if (len(SumOfList) > 1 or (len(SumOfList) == 1 and len(SumOfList[0]) == 3)) and i == len(
SumOfList) - 1 and (NumberInToWord[-1] in LargeMathWords or Hundred in NumberInToWord[-1]):
NumberInToWord.append("and")
NumberInToWord.append(OneDigitWords[number][0])
number = number[1:]
break

if len(number) == 2:
if number[0] != '0':
if (len(SumOfList) > 1 or (len(SumOfList) == 1 and len(SumOfList[0]) == 3)) and i == len(SumOfList) - 1:
NumberInToWord.append("and")
if number.startswith('1'):
if int(number[1]) in range(3):
NumberInToWord.append(TwoDigitWords[int(number[1])])
else:
number = OneDigitWords[number[1]][1 if int(number[1]) in range(3, 6, 2) else 0]
NumberInToWord.append(number + ("teen" if not number[-1] == 't' else "een"))
else:
NumberInToWord.append(OneDigitWords[number[0]][1 if int(number[0]) in range(2, 6) else 0] + (
"ty " if number[0] != '8' else 'y ') + (OneDigitWords[number[1]][0] if number[1] != '0' else ""))
break
else:
number = number[1:]
continue

if len(number) == 3:
if number[0] != '0':
NumberInToWord.append(OneDigitWords[number[0]][0] + " " + Hundred)
if number[1:] == '00': break
number = number[1:]

if len(SumOfList[i:]) > 1 and not skip:
NumberInToWord.append(LargeMathWords[len(SumOfList[i:]) - 2])
skip = True

NumberInToWord = " ".join(map(str.strip, NumberInToWord))
return NumberInToWord[0].lstrip().upper() + NumberInToWord[1:].rstrip().lower() if "negative" not in NumberInToWord else NumberInToWord[:11].lstrip() + \
NumberInToWord[11].upper() + NumberInToWord[
12:].rstrip().lower()


if __name__ == "__main__":
while True:
try:
InputNumber = input("\n Enter any number to convert it into words or 'x' to stop: ")
if InputNumber == "x" or InputNumber == "X":
break
int(InputNumber)
print(InputNumber, ": In Word :", NumberInToWordConverter(InputNumber))
except ValueError:
print("Error: You type an invalid number!")

Sample Input:

123456789
-900
66666
x

Sample Output:

123456789 : In Word : One hundred twenty three million four hundred fifty six thousand seven hundred and eighty nine


900 : In Word : (negative) nine hundred

66666 : In Word : Sixty six thousand six hundred and sixty six

program terminated


Understand the program: "Number to word converter -  A python project for practice"


Large number name rule

LargeMathWords = ["thousand", "million", "billion", "trillion", "quadrillion", "quintillion", "sextillion", "septillion", "octillion", "nonillion"]

Number rules (dictionary initialization)

OneDigitWords = {
'0': ["zero"],
'1': ["one"],
'2': ["two", "twen"],
'3': ["three", "thir"],
'4': ["four", "for"],
'5': ["five", "fif"],
'6': ["six"],
'7': ["seven"],
'8': ["eight"],
'9': ["nine"],
}
TwoDigitWords = ["ten", "eleven", "twelve"]
Hundred = "hundred"

Number rules (zero rules)

'0': ["zero"],

Number rules (3-digit rule)

if (len(SumOfList) > 1 or (len(SumOfList) == 1 and len(SumOfList[0]) == 3)) and i == len(
SumOfList) - 1 and (NumberInToWord[-1] in LargeMathWords or Hundred in NumberInToWord[-1]):
if len(SumOfList[i:]) > 1 and not skip:
NumberInToWord.append(LargeMathWords[len(SumOfList[i:]) - 2])
skip = True

Number rules (hundred rules)

if len(number) == 3:
if number[0] != '0':
NumberInToWord.append(OneDigitWords[number[0]][0] + " " + Hundred)
if number[1:] == '00': break
number = number[1:]

Number rules (tens rules)

number = OneDigitWords[number[1]][1 if int(number[1]) in range(3, 6, 2) else 0]
NumberInToWord.append(number + ("teen" if not number[-1] == 't' else "een"))

Number rules (Recombination or Comma rule)

(OneDigitWords[number[1]][0] if number[1] != '0' else ""))
if (len(SumOfList) > 1 or (len(SumOfList) == 1 and len(SumOfList[0]) == 3)) and i == len(
SumOfList) - 1 and (NumberInToWord[-1] in LargeMathWords or Hundred in NumberInToWord[-1]):
NumberInToWord.append("and")

Number rules (Negative rule)

def IsNumberNegative(NumberInToWord, InputNumber):
if InputNumber.startswith('-'):
NumberInToWord.append("(Negative)")
InputNumber = InputNumber[1:]
return NumberInToWord, InputNumber


A recent python cool project has been discussed in this link.

If you wish to extend the functionality of the program, you should read and follow this article on Wikipedia.

Post a Comment

Previous Post Next Post