Convert Integer to English Words
StringsAuto-graded · 30 test casesLast updated
Tests ability to decompose integers into groups and translate numeric values into properly formatted English text with correct scaling terms.
Write a function that converts a nonnegative integer into its English words representation.
Your function must handle all integers from 0 to 2,147,483,647 (inclusive).
Rules:
- The input
0should produce"Zero" - Use American English scale terms:
Thousand,Million,Billion - Do not include the word
andanywhere in the output - Separate words with single spaces; no leading or trailing whitespace
- Capitalize the first letter of each word exactly as shown below
Constraints:
0 ≤ num ≤ 2,147,483,647
Examples
Example 1
Input: number_to_words(7)
Output: 'Seven'
Example 2
Input: number_to_words(19)
Output: 'Nineteen'
Example 3
Input: number_to_words(123)
Output: 'One Hundred Twenty Three'
Example 4
Input: number_to_words(1005)
Output: 'One Thousand Five'
Example 5
Input: number_to_words(1234567)
Output: 'One Million Two Hundred Thirty Four Thousand Five Hundred Sixty Seven'
Example 6
Input: number_to_words(2147483647)
Output: 'Two Billion One Hundred Forty Seven Million Four Hundred Eighty Three Thousand Six Hundred Forty Seven'
Hints
Hint 1
Think of the number as groups of three digits from right to left, each with an optional scale word (Thousand, Million, Billion).
Hint 2
Build a helper function that converts any number from 1 to 999 into words—this handles the irregular patterns for tens and teens.
Hint 3
Collect all word parts in a list and join them at the end to avoid stray spaces.
Worked solution
Easy Try it yourself first — then compare your approach, complexity and edge cases.
Approach
English expresses large numbers by breaking them into three-digit chunks from right to left, pronouncing each chunk the same way, then appending a scale term (Billion, Million, Thousand, or nothing for the units).
For example, 1,234,567 becomes:
1(millions place) → "One Million"234(thousands place) → "Two Hundred Thirty Four Thousand"567(units place) → "Five Hundred Sixty Seven"
The solution has two parts:
-
Helper function for 1–999: Handle the irregular English patterns:
- Hundreds digit: lookup and add "Hundred"
- Remainder 20–99: lookup tens word, then add ones digit if nonzero
- Remainder 10–19: lookup from teens table (these are irregular)
- Remainder 1–9: lookup ones word
- Zero remainder: emit nothing
-
Main function: Extract each three-digit group from largest scale to smallest. If a group is nonzero, convert it with the helper and append its scale term. Skip groups that are zero entirely (this prevents output like "Zero Thousand"). Handle input
0as a special case returning "Zero".
Building the output as a list of words and joining once at the end ensures correct spacing without manual concatenation logic.
Why It Works
Each scale divisor (1 billion, 1 million, 1 thousand, 1) extracts exactly one three-digit group. Since we process from largest to smallest and use modulo to remove processed digits, we never see the same digit twice. The helper function covers all values 1–999 with non-overlapping branches, and zero remainders correctly produce no output (e.g., 300 becomes "Three Hundred" with no trailing "Zero").
Complexity
- Time: O(1) — The input range is bounded, requiring at most four groups with constant work per group. Without the bound, this would be O(log n) in the number of digits.
- Space: O(1) — Fixed-size lookup tables and output list with at most ~20 words.
Solution
ONES = ["", "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine"]
TEENS = ["Ten", "Eleven", "Twelve", "Thirteen", "Fourteen", "Fifteen", "Sixteen", "Seventeen", "Eighteen", "Nineteen"]
TENS = ["", "", "Twenty", "Thirty", "Forty", "Fifty", "Sixty", "Seventy", "Eighty", "Ninety"]
def convert_below_thousand(num):
"""Convert a number from 1 to 999 into a list of words."""
result = []
if num >= 100:
result.extend([ONES[num // 100], "Hundred"])
num %= 100
if num >= 20:
result.append(TENS[num // 10])
num %= 10
if num:
result.append(ONES[num])
elif num >= 10:
result.append(TEENS[num - 10])
elif num:
result.append(ONES[num])
return result
def number_to_words(num: int) -> str:
if num == 0:
return "Zero"
words = []
scale_groups = [(1_000_000_000, "Billion"), (1_000_000, "Million"), (1_000, "Thousand"), (1, "")]
for divisor, scale_name in scale_groups:
chunk = num // divisor
if chunk:
words.extend(convert_below_thousand(chunk))
if scale_name:
words.append(scale_name)
num %= divisor
return " ".join(words)