Skip to content

Instantly share code, notes, and snippets.

@shiracamus
Last active January 13, 2018 21:41
Show Gist options
  • Save shiracamus/4bbb10293331c9fb72150612e099d915 to your computer and use it in GitHub Desktop.
Save shiracamus/4bbb10293331c9fb72150612e099d915 to your computer and use it in GitHub Desktop.
words = {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: 'Forty', 50: 'Fifty', 60: 'Sixty',
70: 'Seventy', 80: 'Eighty', 90: 'Ninety', 100: 'Hundred',
1000: 'Thousand', 1000000: 'Million', 1000000000: 'Billion'}
units = sorted(words, reverse=True)
def number2word(number):
if number == 0:
return 'Zero'
def convert(number):
if number < 0:
yield 'Negative'
number *= -1
for unit in units:
if number >= unit:
if unit >= 100:
yield number2word(number // unit) + ' ' + words[unit]
number %= unit
else:
yield words[unit]
number -= unit
return ' '.join(convert(number))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment