Skip to content

Commit a9eeae9

Browse files
committed
Add string to number converter
1 parent a71618f commit a9eeae9

File tree

1 file changed

+107
-0
lines changed

1 file changed

+107
-0
lines changed

strings/string_to_num.py

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
''' Converts a given string to integer and float
2+
This works with only Indian system of wording
3+
4+
* Indian system uses crore, lakh, thousand and not million and billions
5+
6+
For the part after the decimal example ( .159 ):
7+
* Digit by digit ( .159 ) -> point one five nine is allowed anything else will throw an
8+
error
9+
10+
>>> "Five"
11+
out:- <class 'int'> --> 5
12+
out:- <class 'float'> --> 5.0
13+
14+
>>> "One thousand five hundred and two"
15+
out:- <class 'int'> --> 1502
16+
out:- <class 'float'> --> 1502.0
17+
18+
>>> "Ninety nine crore three lakh seventy two thousand and six point one five nine"
19+
out:- <class 'int'> --> 990372006
20+
out:- <class 'float'> --> 990372006.159
21+
22+
'''
23+
24+
def to_int(word):
25+
26+
if len(word.strip())>0:
27+
units = {"zero":0,"one":1,"two":2,"three":3,"four":4,"five":5,"six":6,"seven":7,"eight":8,"nine":9,"eleven":11,"twelve":12,"thirteen":13,"fourteen":14,"fifteen":15,"sixteen":16,"seventeen":17,"eighteen":18,"nineteen":19}
28+
29+
tens = {"ten":10,"twenty":20,"thirty":30,"forty":40,"fifty":50,"sixty":60,"seventy":70,"eighty":80,"ninety":90}
30+
31+
multiplyers = {"hundred":100,"thousand":1_000,"lakh":1_00_000,"crore":1_00_00_000}
32+
33+
if "point" in word:
34+
word_lst = word.split("point")
35+
word = "".join(word_lst[:-1])
36+
37+
words = word.strip().replace(" and","").replace("-","").replace("_","").lower().split()
38+
39+
number = 0 #
40+
temp = 0 #
41+
42+
for index,word in enumerate(words):
43+
if index == 0:
44+
if word in units:
45+
temp += units[word]
46+
elif word in tens:
47+
temp += tens[word]
48+
else:
49+
temp += multiplyers[word]
50+
elif index == (len(words)-1):
51+
if word in units:
52+
temp += units[word]
53+
number += temp
54+
elif word in tens:
55+
temp += tens[word]
56+
number += temp
57+
else:
58+
temp *= multiplyers[word]
59+
number += temp
60+
elif word in units:
61+
temp += units[word]
62+
elif word in tens:
63+
temp += tens[word]
64+
elif word in multiplyers:
65+
temp *= multiplyers[word]
66+
number += temp
67+
temp = 0
68+
69+
if len(words)>1:
70+
return(number)
71+
else:
72+
return(temp)
73+
else:
74+
raise ValueError("Empty input is not a valid number in words")
75+
76+
def to_float(word):
77+
units = {"zero":0,"one":1,"two":2,"three":3,"four":4,"five":5,"six":6,"seven":7,"eight":8,"nine":9,"eleven":11,"twelve":12,"thirteen":13,"fourteen":14,"fifteen":15,"sixteen":16,"seventeen":17,"eighteen":18,"ninteen":19}
78+
79+
all_words = word.strip().replace(" and","").replace("-","").replace("_","").lower().split("point")
80+
if len(all_words)>1:
81+
word = all_words[0]
82+
after_point = all_words[1].split()
83+
84+
integer_part = to_int(word)
85+
86+
decimal_part = ""
87+
for num in after_point:
88+
if num in units:
89+
decimal_part += str(units[num])
90+
91+
str_float = str(integer_part) + str(decimal_part)
92+
divider = ("1" + ("0" * len(after_point)))
93+
return (int(str_float)/int(divider))
94+
95+
else:
96+
return(float(to_int(word)))
97+
98+
if __name__ == "__main__":
99+
while True:
100+
word = input("Enter a number in words (q to quit) :- ").lower().strip()
101+
if word == "q":
102+
break
103+
else:
104+
integer = to_int(word)
105+
print(f"\nThe number in {type(integer)} --> {integer} ")
106+
floater = to_float(word)
107+
print(f"\nThe number in {type(floater)} --> {floater} ")

0 commit comments

Comments
 (0)