forked from SidharthMudgil/mini-projects
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnumber-to-words.py
More file actions
84 lines (63 loc) · 2.57 KB
/
Copy pathnumber-to-words.py
File metadata and controls
84 lines (63 loc) · 2.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
def three_digit(num):
ones = ['one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine']
tens = ['ten', 'twenty', 'thirty', 'forty', 'fifty', 'sixty', 'seventy', 'eighty', 'ninety']
name = ['eleven', 'twelve', 'thirteen', 'fourteen', 'fifteen', 'sixteen', 'seventeen', 'eighteen', 'nineteen']
rem = quo = 0
# Will find length of number using len function by temporary converting into string
digit = len(str(num))
# Splitting hundredth place and up to tens place
if digit == 3:
x = num // 100 # hundredth place number
num = num - (x * 100) # tens and ones place number
print(ones[x - 1], "hundred ", end='')
# For 100, 200, ...
if num == 0:
return ""
quo = num // 10 # Will find quotient of number
rem = num % 10 # Will fund remainder of number
# One digit Number
if quo == 0:
return ones[rem - 1]
# Two digit number ending with zero
elif rem == 0:
return tens[quo - 1]
# Two digit starting from 1 except 10
elif quo == 1 and rem != 0:
return name[rem - 1]
# Others two digit numbers
else:
return f"{tens[quo - 1]}-{ones[rem - 1]}"
def split_up(num):
units = ['thousand', 'million', 'billion', 'trillion', 'quadrillion', 'quintillion', 'sextillion', 'septillion']
# If number is zero it will return zero and if number is negative it will convert into positive
if num == 0:
return "zero"
elif num < 0:
num = -num
print("Negative of", end=" ")
# Count the digits of number
digit = len(str(num))
# Will work if number is greater than 999
while digit > 3:
# Total number of 3-3 digits pair in the number ex: 12345 -> term = 1
term = 0
# Find total number of terms multiplied by 3
if digit > 3 and digit % 3 != 0:
term = (digit // 3) * 3
else:
term = ((digit // 3) - 1) * 3
# Split number into two for ex: 1234 -> (1, 234); 123456 -> (123,456)
front_3 = num // (10 ** term) # front three digit
num = num - front_3 * (10 ** term) # left digit
# storing text for number of front part with its place 1234 -> 1 -> one thousand
word = three_digit(front_3) + " " + units[term // 3 - 1]
print(word, end=' ')
digit = len(str(num))
# For numbers less than 1000
return three_digit(num)
if __name__ == "__main__":
try:
number = int(input("Enter Number : "))
print(split_up(number))
except ValueError:
print("Only Numbers Allowed")