Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

number to words #5162

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions Python Projects/Number_to_Words/ReadMe.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# Number to words
This python code takes a number as input and returns its word name.
77 changes: 77 additions & 0 deletions Python Projects/Number_to_Words/numberToWords.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
import tkinter as tk
from tkinter.filedialog import *

global groups

first_twenty = ["", "One ", "Two ", "Three ", "Four ", "Five ", "Six ", "Seven ", "Eight ", "Nine ", "Ten ",
"Eleven ", "Twelve ", "Thirteen ", "Fourteen ", "Fifteen ", "Sixteen ", "Seventeen ", "Eighteen ", "Nineteen ", "Twenty "]

tens = ["","","twenty ","thirty ", "fourty ","fifty ","sixty ","seventy ","eighty ","ninety "]
hundreds = ["","One hundred ", "Two hundred ","Three hundred ","Four hundred ","Five hundred ",
"Six hundred ","Seven hundred ","Eight hundred ","Nine hundred " ]

mult = ["","thousand", "million", "billion"]


window = tk.Tk()
window.title("Number to words")
window.geometry('300x100')

def count_dig(number): # function to count numner of digits
sum = 0
while(number>0):
sum += 1
number = number//10
return sum


def numof3dig(samp): # finding name of a set of 3 digits
answer = ""
if count_dig(samp)==3 and samp%100!=0:
while samp>0:
answer = hundreds[samp//100]
if samp%100<=20:
answer = answer + first_twenty[samp%100]
else:
answer = answer + hundreds[(samp//100)] + tens[((samp%100)//10)] + first_twenty[samp%10]
elif count_dig(samp)==3 and samp%100==0:
answer = answer + hundreds[(samp//100)]
else:
answer = answer + tens[((samp%100)//10)] + first_twenty[samp%10]
return answer

def number_words(n):
global groups
fin_answer = ""
ncopy = n
groups = 0
if(ncopy == 0):
fin_answer = "zero"
elif(ncopy>0 and ncopy<=20):
fin_answer = first_twenty[num]
label = tk.Label(window, text="Output: ").grid(row=2, column=0)
label = tk.Label(window, text=fin_answer).grid(row=2, column=1)
return
while(ncopy>0):
fin_answer = numof3dig(ncopy%1000) + mult[groups] + fin_answer
groups += 1
ncopy = ncopy//1000
label = tk.Label(window, text="Output: ").grid(row=2, column=0)
label = tk.Label(window, text=fin_answer).grid(row=2, column=1)
return


def proceeds():
global num
num = t.get(1.0, "end-1c")
num = int(num)
number_words(num)

label = tk.Label(window, text="Enter a number").grid(row=0, column=0)
t = tk.Text(window, height=1, width=15)
proceed = tk.Button(window, text='Proceed', command=proceeds)

t.grid(row=0,column=1)
proceed.grid(row=1,column=1)

window.mainloop()
Binary file added Python Projects/Number_to_Words/sample.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.