Numbers to Words

Level: Beginner 15–30 min

Concepts: Strings

Solutions: C# | TypeScript | Python


There are varying opinions amongst writers as to when one should spell out a number vs. using figures. However there are a few generally agreed upon rules for doing so.

There are 2 of them we will follow for this kata:

  • Numbers beginning a sentence should be spelt out.
  • Hyphenate all compound numbers from twenty-one through ninety-nine

Write an implementation of these rules for numbers starting a sentence. It should take an input of figures and return the number spelt out. Assume all the numbers given start sentences and your code will be used like a spell checker for grammar issues related to numbers.

Only worry about up numbers up to 4 digits long. Assume all numbers are positive.

Examples

1 digit 2 digits 3 digits 4 digits
0 = zero 10 = ten 100 = one hundred 2000 = two thousand
5 = five 21 = twenty-one 303 = three hundred three 3466 = three thousand four hundred sixty-six
8 = eight 77 = seventy-seven 555 = five hundred fifty-five 2400 = two thousand four hundred

Hint

Solve for 1 digit, then move to 2 digits and so on.

Bonus

The simplest way to express large numbers is usually best. Instead of 5300 being equal to five thousand three hundred it should return fifty-three hundred. This new rule only applies to 4 digit numbers.

Reference Walkthrough

Reference implementations in C#, TypeScript, and Python live at tddbuddy-reference-katas/numbers-to-words. Fifteen scenarios cover one-, two-, three-, and four-digit cases — teens that are a single word (nineteen), hyphenated compounds from twenty-one through ninety-nine, hundreds that name the leading one (one hundred), and the “no ‘and’” join between hundreds and the remainder (three hundred three, not three hundred and three). The API in all three languages is the same shape: toWords(n) for TypeScript, ToWords(int n) for C#, to_words(n) for Python — each returning the English-words spelling for 0..9999. The bonus “fifty-three hundred” four-digit shorthand is out of scope; the reference uses the canonical "five thousand three hundred" form.

This kata ships in Agent Full-Bake mode at high gear (F1 tier): a pure integer-to-string transform small enough to land as one commit per language, with a brief walkthrough noting there are no builders because the input (an int) and output (a string) are the domain. Two lookup tables — ones for 0..19 (so teens are one word) and tens for twenty..ninety — plus a thousands/hundreds/sub-hundred split carry the whole grammar. See the repo’s Gears section for when high gear is the right call.


  • Katas Are Rehearsal, Not Performance
    You don't practice TDD on production code. You practice on katas and bring the muscle memory to production. The gap between knowing TDD and doing TDD is reps.
  • The Test Pyramid Was an Economic Argument
    The test pyramid was not a quality law. It was a cost structure: unit tests were cheap, integration tests were expensive, so you wrote many of the first and few of the second. Agents collapsed the cost of writing tests at every level, and the cheapest test that still tells the truth is the one that pins a seam the agent cannot fake.
  • Agents Pattern-Match Your Test Smells
    Test smells used to drift in at typing speed. Agents reproduce them at generation speed because they sample whatever is already in the suite. The smells in your codebase are now the smells in your roadmap.