Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Integer to English Words in Python Programming
Converting integers to their English word representation is a common programming challenge. In Python, we can solve this by breaking down numbers into groups of three digits and converting each group separately using predefined word mappings.
Approach
To convert a number to English words, we follow these steps:
Create arrays for numbers less than 20, tens (20, 30, 40, etc.), and scale words (thousand, million, billion)
Use a helper function to convert numbers less than 1000 to words
Process the number in groups of three digits from right to left
Combine the converted groups with appropriate scale words
Implementation
class NumberToWords:
def __init__(self):
self.less_than_20 = ["", "One", "Two", "Three", "Four", "Five", "Six",
"Seven", "Eight", "Nine", "Ten", "Eleven", "Twelve",
"Thirteen", "Fourteen", "Fifteen", "Sixteen",
"Seventeen", "Eighteen", "Nineteen"]
self.tens = ["", "Ten", "Twenty", "Thirty", "Forty", "Fifty", "Sixty",
"Seventy", "Eighty", "Ninety"]
self.thousands = ["", "Thousand", "Million", "Billion"]
def numberToWords(self, num):
if num == 0:
return "Zero"
result = ""
group_index = 0
while num > 0:
if num % 1000 != 0:
group_words = self.helper(num % 1000)
scale_word = self.thousands[group_index]
if scale_word:
group_words += scale_word + " "
result = group_words + result
group_index += 1
num //= 1000
return result.strip()
def helper(self, n):
if n == 0:
return ""
elif n < 20:
return self.less_than_20[n] + " "
elif n < 100:
return self.tens[n // 10] + " " + self.helper(n % 10)
else:
return self.less_than_20[n // 100] + " Hundred " + self.helper(n % 100)
# Test the implementation
converter = NumberToWords()
print(converter.numberToWords(512))
print(converter.numberToWords(7835271))
print(converter.numberToWords(0))
print(converter.numberToWords(1234567890))
Five Hundred Twelve Seven Million Eight Hundred Thirty Five Thousand Two Hundred Seventy One Zero One Billion Two Hundred Thirty Four Million Five Hundred Sixty Seven Thousand Eight Hundred Ninety
How It Works
The algorithm processes numbers in groups of three digits:
Helper function: Converts numbers 0-999 to words using the predefined arrays
Main function: Splits the input number into groups of 1000 and applies the helper function to each group
Scale words: Adds "Thousand", "Million", or "Billion" as appropriate
Key Features
Handles numbers from 0 to 2³¹ - 1 (over 2 billion)
Uses recursive approach for numbers within each group
Properly formats output with correct spacing
Handles edge case of zero explicitly
Conclusion
This solution efficiently converts integers to English words by breaking down the problem into smaller, manageable chunks. The key insight is processing numbers in groups of three digits and using predefined word mappings for consistent results.
