Advertisement
acclivity

pyInteger2Words

Apr 27th, 2023
1,194
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.47 KB | Software | 0 0
  1. # Convert an integer to words
  2. # e.g 1234 -> One thousand two hundred and thirty four
  3.  
  4.  
  5. def int2words(num):
  6.     w1 = ["", "one ", "two ", "three ", "four ", "five ", "six ", "seven ", "eight ", "nine "]
  7.     w2 = ["", "teen ", "twenty ", "thirty ",  "forty ", "fifty ", "sixty ", "seventy ", "eighty ", "ninety "]
  8.     w3 = ["", "ty", "hundred and ", "thousand ", "ty", "hundred and ", "million ", "ty", "hundred and ", "billion ",]
  9.     w4 = ["ten ", "eleven ", "twelve ", "thirteen ", "fourteen ",
  10.           "fifteen ", "sixteen ", "seventeen ", "eighteen", "nineteen"]
  11.     res = ""
  12.     if num >= 9999999999:
  13.         return "Integer is too large"
  14.     snum = str(num)
  15.     i = 0
  16.     while i < len(snum):        # iterates left to right
  17.         j = len(snum) - i - 1
  18.         # j counts backwards. j = 1 gets last 2 digits
  19.         a = snum[i]             # one character string
  20.         s1 = w1[int(a)]         # single digit word
  21.         s2 = w3[j]              # order word
  22.         if a == "0" and s2 == "hundred and ":
  23.             s2 = "and "
  24.             if j == 5:
  25.                 s2 = ""
  26.  
  27.         if s2 == "ty":
  28.             s1 = w2[int(a)]     # change s1 to xxxty word
  29.             s2 = ""             # remove order word
  30.         if (j == 1 or j == 4) and a < "2":                  # Last 2 digits "00" through "19"
  31.             i += 1
  32.             j -= 1
  33.             y = int(a + snum[i])               # numeric value of last 2 digits
  34.             if y < 10:
  35.                 s1 = w1[y]                     # blank or one through nine
  36.                 if y == 0:
  37.                     if s2 != "":
  38.                         print("**********s2=", s2)
  39.                     # s2 = ""
  40.                     if res[-5:] == "and ":     # Remove "and" if nothing following
  41.                        res = res[:-4]
  42.             else:
  43.                 s1 = w4[y-10]                   # ten through nineteen
  44.                 s2 = ""
  45.             if j == 3 and y > 0:
  46.                 s2 = "thousand "
  47.         res += s1
  48.         res += s2
  49.         i += 1
  50.         if j == 3 or j == 0:
  51.             if res[-5:] == " and ":
  52.                 res = res[:-5]              # there is nothing following 'and' so remove thw 'and'
  53.     if res == "":
  54.         return("zero")
  55.  
  56.     return res
  57.  
  58.  
  59. tests = [1008017, 1461234, 502, 61234, 4300, 56, 160, 9999, 1000, 1001, 1100, 0, 6011, 2055,
  60.          6, 14, 1000017, 1000000, 22000000, 198765432, 2198765432]
  61.  
  62. for t in tests:
  63.     print(str(t).ljust(10), int2words(t))
  64.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement