Skip to content

How to Write Out Numbers in Python

The challenge

Create a function that transforms any positive number to a string representing the number in words. The function should work for all numbers between 0 and 999999.

Examples:

number2words() ==> "zero" number2words(1) ==> "one" number2words(9) ==> "nine" number2words(10) ==> "ten" number2words(17) ==> "seventeen" number2words(20) ==> "twenty" number2words(21) ==> "twenty-one" number2words(45) ==> "forty-five" number2words(80) ==> "eighty" number2words(99) ==> "ninety-nine" number2words(100) ==> "one hundred" number2words(301) ==> "three hundred one" number2words(799) ==> "seven hundred ninety-nine" number2words(800) ==> "eight hundred" number2words(950) ==> "nine hundred fifty" number2words(1000) ==> "one thousand" number2words(1002) ==> "one thousand two" number2words(3051) ==> "three thousand fifty-one" number2words(7200) ==> "seven thousand two hundred" number2words(7219) ==> "seven thousand two hundred nineteen" number2words(8330) ==> "eight thousand three hundred thirty" number2words(99999) ==> "ninety-nine thousand nine hundred ninety-nine" number2words(888888) ==> "eight hundred eighty-eight thousand eight hundred eighty-eight"
Code language: Python (python)

The solution in Python code

Option 1:

words = "zero one two three four five six seven eight nine" + \ " ten eleven twelve thirteen fourteen fifteen sixteen seventeen eighteen nineteen twenty" + \ " thirty forty fifty sixty seventy eighty ninety" words = words.split(" ") def number2words(n): if n < 20: return words[n] elif n < 100: return words[18 + n // 10] + ('' if n % 10 == else '-' + words[n % 10]) elif n < 1000: return number2words(n // 100) + " hundred" + (' ' + number2words(n % 100) if n % 100 > else '') elif n < 1000000: return number2words(n // 1000) + " thousand" + (' ' + number2words(n % 1000) if n % 1000 > else '')
Code language: Python (python)

Option 2:

See also  How to Calculate the Summation of a Number in Java
d={:'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:'forty',50:'fifty', 60:'sixty',70:'seventy',80:'eighty',90:'ninety', 100:'hundred',1000:'thousand'} def number2words(n): """ works for numbers between 0 and 999999 """ if <=n<=20:return d[n] if 20<n<=99 and n%10:return d[10*(n//10)]+'-'+d[n%10] if 20<n<99:return d[10*(n//10)] if n<1000 and n%100==:return d[n//100]+' '+d[100] if 100<n<=999:return d[n//100]+' '+d[100]+' '+number2words(n%100) if n%1000==:return d[n//1000]+' '+d[1000] return number2words(n//1000)+' '+d[1000]+' '+number2words(n%1000)
Code language: Python (python)

Option 3:

d = {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', :''} def number2words(n): s = (htu(n // 1000) + ' thousand ' if n // 1000 else '') + htu(n % 1000) return ' '.join(s.split()) if s else 'zero' def htu(n): h, tu, u = n//100, n % 100, n % 10 t = (d[tu] if tu in d else d[tu//10*10] + '-' + d[u]).strip('-') return d[h] + ' hundred ' + t if h else t
Code language: Python (python)

Test cases to validate our solution

from solution import number2words import test @test.describe("fixed tests") def _(): @test.it("One digit Tests") def _(): test.assert_equals(number2words(), "zero") test.assert_equals(number2words(1), "one") test.assert_equals(number2words(8), "eight") @test.it("Tens Tests") def _(): test.assert_equals(number2words(10), "ten") test.assert_equals(number2words(19), "nineteen") test.assert_equals(number2words(20), "twenty") test.assert_equals(number2words(22), "twenty-two") test.assert_equals(number2words(54), "fifty-four") test.assert_equals(number2words(80), "eighty") test.assert_equals(number2words(98), "ninety-eight") @test.it("Hundreds Tests") def _(): test.assert_equals(number2words(100),"one hundred") test.assert_equals(number2words(301),"three hundred one") test.assert_equals(number2words(793),"seven hundred ninety-three") test.assert_equals(number2words(800),"eight hundred") test.assert_equals(number2words(650),"six hundred fifty") @test.it("Thousands Tests") def _(): test.assert_equals(number2words(1000), "one thousand") test.assert_equals(number2words(1003), "one thousand three")
Code language: Python (python)

See also  How to get the Hostname in Python
Tags:
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x