Last Updated on: 11th May 2024, 11:39 am
In the realm of programming, the conversion of numerical data into its corresponding word representation is a common requirement. This is particularly useful when presenting numeric values in a human-readable format, such as generating invoices, printing checks, or creating textual representations of large numerical datasets.
Converting Numbers to Words in Python
The number_to_words
Function
The number_to_words
function in Python serves as the cornerstone for converting numeric values into words. Let’s dive into the details of this function, line by line:
def number_to_words(number):
This line defines the number_to_words
function, which accepts a numeric value as input and returns its word representation as a string.
# Define lists for the words representing numbers ones = ['', 'One', 'Two', 'Three', 'Four', 'Five', 'Six', 'Seven', 'Eight', 'Nine'] teens = ['', 'Eleven', 'Twelve', 'Thirteen', 'Fourteen', 'Fifteen', 'Sixteen', 'Seventeen', 'Eighteen', 'Nineteen'] tens = ['', 'Ten', 'Twenty', 'Thirty', 'Forty', 'Fifty', 'Sixty', 'Seventy', 'Eighty', 'Ninety']
Here, we define lists containing the English words representing single-digit numbers (ones
), numbers in the teens (teens
), and multiples of ten (tens
).
# Define a function to convert the hundreds place def convert_hundreds(num): if num == 0: return '' elif num < 10: return ones[num] + ' Hundred ' else: return ones[num // 100] + ' Hundred ' + convert_tens(num % 100)
The convert_hundreds
function is defined to convert the hundreds of places of a number into its corresponding word representation.
# Define a function to convert the tens and ones places def convert_tens(num): if num < 10: return ones[num] elif num < 20: return teens[num - 10] else: return tens[num // 10] + ' ' + ones[num % 10]
Similarly, the convert_tens
function is defined to convert the tens and ones places of a number into words.
# Handling special cases if number == 0: return 'Zero' elif number < 0: return 'Negative ' + number_to_words(abs(number))
Special cases such as zero and negative numbers are addressed here. If the input number is zero, the function returns the word “Zero.” Negative numbers are converted to their positive equivalents and prefixed with the word “Negative.”
# Main conversion logic words = '' if number >= 1000000000: words += convert_hundreds(number // 1000000000) + ' Billion ' number %= 1000000000
The main conversion logic begins here, where we iterate through each place value of the input number starting from billions down to ones, appending the result to the words
string.
if number >= 1000000: words += convert_hundreds(number // 1000000) + ' Million ' number %= 1000000 if number >= 1000: words += convert_hundreds(number // 1000) + ' Thousand ' number %= 1000
The logic continues by converting millions and thousands of places into words, updating the number accordingly after each conversion.
words += convert_hundreds(number)
Finally, the remaining number (if any) is converted into words representing the hundreds place and appended to the words
string.
return words.strip()
The function returns the words
string after stripping any trailing whitespace, representing the final word representation of the input number.
Usage Example
Here’s an example of how to use the number_to_words
function in Python:
number = int(input("Enter a number: ")) print(number_to_words(number))
Complete Code Python
def number_to_words(number): # Define lists for the words representing numbers ones = ['', 'One', 'Two', 'Three', 'Four', 'Five', 'Six', 'Seven', 'Eight', 'Nine'] teens = ['', 'Eleven', 'Twelve', 'Thirteen', 'Fourteen', 'Fifteen', 'Sixteen', 'Seventeen', 'Eighteen', 'Nineteen'] tens = ['', 'Ten', 'Twenty', 'Thirty', 'Forty', 'Fifty', 'Sixty', 'Seventy', 'Eighty', 'Ninety'] # Define a function to convert the hundreds place def convert_hundreds(num): if num == 0: return '' elif num < 10: return ones[num] + ' Hundred ' else: return ones[num // 100] + ' Hundred ' + convert_tens(num % 100) # Define a function to convert the tens and ones places def convert_tens(num): if num < 10: return ones[num] elif num < 20: return teens[num - 10] else: return tens[num // 10] + ' ' + ones[num % 10] # Handling special cases if number == 0: return 'Zero' elif number < 0: return 'Negative ' + number_to_words(abs(number)) # Main conversion logic words = '' if number >= 1000000000: words += convert_hundreds(number // 1000000000) + ' Billion ' number %= 1000000000 if number >= 1000000: words += convert_hundreds(number // 1000000) + ' Million ' number %= 1000000 if number >= 1000: words += convert_hundreds(number // 1000) + ' Thousand ' number %= 1000 words += convert_hundreds(number) return words.strip() # Test the function number = int(input("Enter a number: ")) print(number_to_words(number))
By leveraging the number_to_words
function in your Python projects, you can enhance the readability and usability of your applications when presenting numerical data in natural language. Whether you’re working on financial applications, generating textual reports, or simply need to display numbers in a human-readable format, this function simplifies the task, making your applications more accessible and user-friendly.
Converting Numbers to Words C++
Complete Code C++
#include <iostream> #include <string> using namespace std; // Define function to convert number to words string number_to_words(long long number) { // Define arrays for the words representing numbers string ones[] = {"", "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine"}; string teens[] = {"", "Eleven", "Twelve", "Thirteen", "Fourteen", "Fifteen", "Sixteen", "Seventeen", "Eighteen", "Nineteen"}; string tens[] = {"", "Ten", "Twenty", "Thirty", "Forty", "Fifty", "Sixty", "Seventy", "Eighty", "Ninety"}; // Define a function to convert the hundreds place string convert_hundreds(long long num) { if (num == 0) { return ""; } else if (num < 10) { return ones[num] + " Hundred "; } else { return ones[num / 100] + " Hundred " + convert_hundreds(num % 100); } } // Define a function to convert the tens and ones places string convert_tens(long long num) { if (num < 10) { return ones[num]; } else if (num < 20) { return teens[num - 10]; } else { return tens[num / 10] + " " + ones[num % 10]; } } // Main conversion logic string words = ""; if (number == 0) { // If the number is zero return "Zero"; } else if (number < 0) { // If the number is negative return "Negative " + number_to_words(-number); // Convert the positive equivalent and add "Negative" } else if (number >= 1000000000) { // If the number is greater than or equal to 1 billion return "Number out of range. Please enter a number up to 1 billion."; } else if (number >= 1000000) { // If the number is greater than or equal to 1 million words += convert_hundreds(number / 1000000) + "Million "; // Convert the millions place number %= 1000000; // Update the number to exclude millions place } else if (number >= 1000) { // If the number is greater than or equal to 1 thousand words += convert_hundreds(number / 1000) + "Thousand "; // Convert the thousands place number %= 1000; // Update the number to exclude thousands place } words += convert_hundreds(number); // Convert the remaining number return words; // Return the final result } int main() { long long number; cout << "Enter a number: "; // Prompt the user to enter a number cin >> number; // Read the number from the user cout << number_to_words(number) << endl; // Convert the number to words and display the result return 0; }