Heh, heh:
class Number:
ones = [
"", "one", "two", "three", "four",
"five", "six", "seven", "eight", "nine",
"ten", "eleven", "twelve", "thirteen", "fourteen",
"fifteen", "sixteen", "seventeen", "eighteen", "nineteen"
]
tens = [
"", "", "twenty", "thirty", "forty",
"fifty", "sixty", "seventy", "eighty", "ninety"
]
powers = [
"",
"thousand",
"million",
"billion",
"trillion",
"quadrillion",
"quintillion",
"sextillion",
"septillion",
"octillion",
"nonillion",
"decillion"
# sufficent to represent any natural number < 10**36
]
max_ = 1000 ** len(powers)
def __init__(self, n=0):
self.n = int(n)
@classmethod
def _chunk_words(cls, n):
"""
Return a string for 0 <= n < 1000
"""
assert 0 <= n < 1000
# get hundreds, tens, ones
c, xi = divmod(n, 100)
x, i = divmod(xi, 10)
# get hundreds string
if c:
sc = cls.ones[c] + " hundred"
else:
sc = ""
# get tens string and ones string
if x < 2:
sx, si = "", cls.ones[10*x + i]
else:
sx, si = cls.tens[x], cls.ones[i]
# get tens-and-ones string
if sx and si:
sxi = sx + " " + si
else:
sxi = sx or si
# get hundreds-and-tens-and-ones string
if sc and sxi:
scxi = sc + " and " + sxi
else:
scxi = sc or sxi
return scxi
def __str__(self):
"""
Return a string representing the number, ie
`str(Number(342))` => `"three hundred and forty two"`
"""
n = self.n
assert 0 <= n < self.max_, "{} is too large (max is {})".format(n, self.max_ - 1)
if n:
# break number into thousands-powers
chunks = []
while n:
n, mod = divmod(n, 1000)
chunks.append(mod)
# combine with powers and reorder; drop 0-powers
chunks = [
(self._chunk_words(ch), pw)
for ch,pw in zip(chunks, self.powers)
if ch
][::-1]
return " ".join(ch + " " + pw if pw else ch for ch,pw in chunks)
else:
return "zero"
then
>>> print(Number(22))
twenty two
>>> print(Number(7598312948732))
seven trillion five hundred and ninety eight billion three hundred and twelve million nine hundred and forty eight thousand seven hundred and thirty two