Skip to content

Instantly share code, notes, and snippets.

@mdpabel
Created August 14, 2022 14:44
Show Gist options
  • Save mdpabel/c50a9026cef902cc8b3aa19a00439448 to your computer and use it in GitHub Desktop.
Save mdpabel/c50a9026cef902cc8b3aa19a00439448 to your computer and use it in GitHub Desktop.
convert integers into words
"""
"41234"
one thousand, two hundred and thirty four
last 2 digits
44 = fourty + four
34 = thirty + four
24 = twinty + four
12 = tweleve
10 = ten
09 = nine
num : {
"1" : "one",
"2" : "two",
}
indexs : {
2 : "hundred",
3 : "thousand",
4 : "thousand"
}
"""
d = {0: 'zero', 1: 'one', 2: 'two', 3: 'three', 4: 'four', 5: 'five',
6: 'six', 7: 'seven', 8: 'eight', 9: 'nine', 10: 'ten',
11: 'eleven', 12: 'twelve', 13: 'thirteen', 14: 'fourteen',
15: 'fifteen', 16: 'sixteen', 17: 'seventeen', 18: 'eighteen',
19: 'nineteen', 20: 'twenty',
30: 'thirty', 40: 'fourty', 50: 'fifty', 60: 'sixty',
70: 'seventy', 80: 'eighty', 90: 'ninety'
}
indexs = {
3: "hundred",
8: "crore",
6: "lac",
4: "thousand"
}
s = "1234"
n = len(s)
s = int(s)
res = ""
isAnd = False
def getStr(n):
res = ""
if len(str(n)) > 1:
if n >= 19:
temp = n % 10
res += d[n - temp]
if temp > 0:
res += " " + d[temp]
else:
res += d[n]
else:
res += d[n]
return res
if len(str(s)) >= 8:
res += getStr(s // 10000000) + " crore, "
s = s % 10000000
if len(str(s)) >= 6:
res += getStr(s // 100000) + " lac, "
s = s % 100000
if len(str(s)) >= 4:
res += getStr(s // 1000) + " thousand, "
s = s % 1000
if len(str(s)) >= 3:
res += getStr(s // 100) + " hundred, "
s = s % 100
if len(str(s)) >= 2:
if res == "":
res += getStr(s)
else:
res += "and " + getStr(s)
print(res)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment