|
| 1 | +# A program to write a number in words |
| 2 | +# Eg: |
| 3 | +# 61893: Sixty One Thousand Eight Hundred Ninety Three |
| 4 | + |
| 5 | +__import__('os').system('cls') |
| 6 | + |
| 7 | + |
| 8 | +Y = "\033[38;2;255;200;0m" |
| 9 | +W = "\033[38;2;212;212;212;0m" |
| 10 | +B = "\033[38;2;108;180;238m;0m" |
| 11 | + |
| 12 | +groupedList = [] |
| 13 | +name = "" |
| 14 | +nameList = [] |
| 15 | + |
| 16 | +numDict = { |
| 17 | + 1 : "One", 2 : "Two", 3 : "Three", 4 : "Four", 5 : "Five", |
| 18 | + 6 : "Six", 7 : "Seven", 8 : "Eight", 9 : "Nine", 10 : "Ten", |
| 19 | + 11 : "Eleven", 12 : "Twelve", 13 : "Thirteen", 14 : "Fourteen", 15 : "Fifteen", |
| 20 | + 16 : "Sixteen", 17 : "Seventeen", 18 : "Eighteen", 19 : "Ninteen", 20 : "Twenty", |
| 21 | + 30 : "Thirty", 40 : "Forty", 50 : "Fifty", 60 : "Sixty", 70 : "Seventy", |
| 22 | + 80 : "Eighty", 90 : "Ninety" |
| 23 | +} |
| 24 | + |
| 25 | +digits = { |
| 26 | + "1" : "One", "2" : "Two", "3" : "Three", "4" : "Four", "5" : "Five", |
| 27 | + "6" : "Six", "7" : "Seven", "8" : "Eight", "9" : "Nine", "0" : "Zero" |
| 28 | +} |
| 29 | + |
| 30 | +placeValueDict = { |
| 31 | + 1 : "", |
| 32 | + 2 : "Thousand", |
| 33 | + 3 : "Million", |
| 34 | + 4 : "Billion", |
| 35 | + 5 : "Trillion", |
| 36 | + 6 : "Quadrillion", |
| 37 | + 7 : "Quintillion", |
| 38 | + 8 : "Sextillion", |
| 39 | + 9 : "Septilion", |
| 40 | + 10 : "Octillion" |
| 41 | +} |
| 42 | + |
| 43 | +print("Maximum Input: 999,999,999,999,999,999,999,999,999,999") |
| 44 | +print("Minimum Input: -999,999,999,999,999,999,999,999,999,999\n") |
| 45 | + |
| 46 | +isNegative = False |
| 47 | + |
| 48 | +while True: |
| 49 | + num = input(f"Enter a number: {Y}") |
| 50 | + print(f"{W}", end="") |
| 51 | + |
| 52 | + try: |
| 53 | + splittedNum = num.split(".") |
| 54 | + |
| 55 | + splittedNum[0] = splittedNum[0].replace(" ", "") |
| 56 | + if len(splittedNum) == 2: |
| 57 | + splittedNum[1] = splittedNum[1].replace(" ", "") |
| 58 | + splittedNum[1] = splittedNum[1].rstrip("0") |
| 59 | + |
| 60 | + if splittedNum[1] == "": |
| 61 | + splittedNum.remove("") |
| 62 | + |
| 63 | + num = int(splittedNum[0]) |
| 64 | + |
| 65 | + if len(splittedNum) == 1: |
| 66 | + placeholder = splittedNum[0] |
| 67 | + placeholder = int(placeholder) |
| 68 | + else: |
| 69 | + placeholder = splittedNum[0] + "." + splittedNum[1] |
| 70 | + placeholder = float(placeholder) |
| 71 | + |
| 72 | + if num >= 1000000000000000000000000000000 or num <= -1000000000000000000000000000000: |
| 73 | + print("Input out of range\n") |
| 74 | + else: |
| 75 | + if num < 0: |
| 76 | + isNegative = True |
| 77 | + num = num * (-1) |
| 78 | + break |
| 79 | + except ValueError or EOFError: |
| 80 | + print("Invalid Input\n") |
| 81 | + |
| 82 | + |
| 83 | +if num == 0: |
| 84 | + print(f"0 in words is: {Y}Zero{W}") |
| 85 | +else: |
| 86 | + while num > 0: |
| 87 | + groupedList.append(num % 1000) |
| 88 | + num //= 1000 |
| 89 | + |
| 90 | + groupedList.reverse() |
| 91 | + |
| 92 | + for i in groupedList: |
| 93 | + if i != 0: |
| 94 | + if i >= 100: |
| 95 | + name = name + numDict[int(i/100)] + " Hundred" |
| 96 | + i = i % 100 |
| 97 | + |
| 98 | + if i >= 20: |
| 99 | + if name == "": |
| 100 | + name = name + numDict[i - (i % 10)] |
| 101 | + else: |
| 102 | + name = name + " " + numDict[i - (i % 10)] |
| 103 | + |
| 104 | + i = i % 10 |
| 105 | + elif i >= 10: |
| 106 | + if name == "": |
| 107 | + name = name + numDict[i] |
| 108 | + else: |
| 109 | + name = name + " " + numDict[i] |
| 110 | + |
| 111 | + i = i % 10 |
| 112 | + |
| 113 | + if i != 0: |
| 114 | + if name == "": |
| 115 | + name = name + numDict[i] |
| 116 | + else: |
| 117 | + name = name + " " + numDict[i] |
| 118 | + |
| 119 | + nameList.append(name) |
| 120 | + name = "" |
| 121 | + else: |
| 122 | + nameList.append("") |
| 123 | + |
| 124 | + for i in range(len(groupedList)): |
| 125 | + if nameList[i] != "": |
| 126 | + name = name + nameList[i] + " " + placeValueDict[len(groupedList) - i] + " " |
| 127 | + |
| 128 | + name = name.rstrip() |
| 129 | + |
| 130 | + if len(splittedNum) == 2 and splittedNum[1] != "": |
| 131 | + name = name + f" {B}Point{Y}" |
| 132 | + |
| 133 | + for i in splittedNum[1]: |
| 134 | + name = name + " " + digits[i] |
| 135 | + |
| 136 | + print(f"{W}", end="") |
| 137 | + |
| 138 | + if isNegative == False: |
| 139 | + print(f"\n{placeholder} in words is: {Y}{name}{W}") |
| 140 | + else: |
| 141 | + print(f"\n{placeholder} in words is: {Y}Minus {name}{W}") |
0 commit comments