Converting Numbers to Words in Python using num2words Library

Faraz

By Faraz - March 27, 2024

Learn how to effortlessly convert numbers to words in Python with this beginner-friendly tutorial. Explore the num2words library for accurate English word representations of numeric values.


Converting Numbers to Words in Python using num2words Library.webp

If you've ever dealt with numeric data in Python and needed to convert it into words for better readability or presentation purposes, you might have found yourself writing custom functions or algorithms to achieve this. However, Python developers have a handy tool at their disposal โ€“ the num2words library โ€“ which simplifies converting numbers into words. In this article, we'll explore how to use the num2words library effectively to convert numbers to words in Python effortlessly.

Table of Contents

  1. Introduction to num2words Library
  2. Installation of num2words Library
  3. Converting Integers to Words
  4. Converting Floating-Point Numbers to Words
  5. Full Numbers to Words Source Code
  6. Explanation of Numbers to Words Source Code
  7. Benefits of Using num2words Library
  8. Conclusion
  9. Frequently Asked Questions (FAQs)

1. Introduction to num2words Library

The num2words library is a Python package that provides functions to convert numbers into their word representations. It is particularly useful when dealing with financial applications, generating reports, or any scenario where numerical data needs to be presented in a more human-readable format.

2. Installation of num2words Library

Before we begin using the num2words library, we need to install it. Installation can be done using pip, Python's package installer, with a simple command:

pip install num2words

3. Converting Integers to Words

Let's start by converting integers to words. With num2words, this task is incredibly simple. We can directly pass an integer to the num2words function, and it will return the textual representation of that number.

from num2words import num2words

number = 12345
words = num2words(number)
print(words)  # Output: twelve thousand three hundred forty-five

4. Converting Floating-Point Numbers to Words

Similarly, num2words can handle floating-point numbers seamlessly. Whether it's a simple decimal or a complex floating-point number, num2words convert it accurately into words.

number = 123.45
words = num2words(number)
print(words)  # Output: one hundred twenty-three point four five

5. Full Numbers to Words Source Code

from num2words import num2words  # pip install num2words

num = int(input("Enter the number : "))

print(
    f"{num} in normal format: {num2words(num)}"
)  
print(
    f"{num} in ordinal format: {num2words(num, to='ordinal')}"
)  
print(
    f"{num} in ordinal number format: {num2words(num, to='ordinal_num')}"
)  
print(
    f"{num} in year format: {num2words(num, to='year')}"
)  
print(
    f"{num} in currency format: {num2words(num, to='currency')}"
)  

languages = {
    "en": "English",
    "ar": "Arabic",
    "cz": "Czech",
    "de": "German",
    "dk": "Danish",
    "es": "Spanish",
    "fr": "French",
    "he": "Hebrew",
    "id": "Indonesian",
    "it": "Italian",
    "ja": "Japanese",
    "kn": "Kannada",
    "ko": "Korean",
    "pt": "Portuguese",
    "ru": "Russian",
    "vi": "Vietnamese",
    "nl": "Dutch",
    "uk": "Ukrainian",
}
print("\nNumber word in different languages:\n")
for (
    language_code,
    language_name,
) in languages.items(): 
    print(f"{num} in {language_name} language is {num2words(num, lang=language_code)}")

6. Explanation of Numbers to Words Source Code

Let's break down the Python code step by step:

1. The code imports the num2words function from the num2words library, which needs to be installed using pip (pip install num2words).

2. It prompts the user to input a number, which is then converted to an integer and stored in the variable num.

3. The code then demonstrates various formatting options provided by num2words:

  • num2words(num): Converts the number to its word representation in the 'normal' format.
  • num2words(num, to='ordinal'): Converts the number to its ordinal form (e.g., first, second, third).
  • num2words(num, to='ordinal_num'): Converts the number to its ordinal number format (e.g., 1st, 2nd, 3rd).
  • num2words(num, to='year'): Converts the number to its word representation in the 'year' format.
  • num2words(num, to='currency'): Converts the number to its word representation in the 'currency' format.

4. The code then defines a dictionary called languages, which maps language codes to their corresponding names. This dictionary represents the languages supported by the num2words library.

5. It iterates over the language dictionary and prints the number converted into words in each supported language using the num2words function with the lang parameter.

7. Benefits of Using num2words Library

  • Saves time and effort by eliminating the need to write custom conversion algorithms
  • Provides accurate and reliable conversion of numbers into words
  • Offers flexibility with customization options for different use cases

8. Conclusion

In conclusion, the num2words library is a valuable tool for Python developers needing to convert numbers into words efficiently. Whether you're working on financial applications, generating reports, or simply enhancing the readability of numeric data, num2words simplifies the process with its intuitive interface and comprehensive functionalities.

9. Frequently Asked Questions (FAQs)

Q1. Is the num2words library compatible with Python 3.x?

Yes, the num2words library is compatible with Python 3.x versions.

Q2. Can I use num2words to convert numbers into currency words?

Absolutely! The num2words library provides functions specifically for converting numbers into currency words.

Q3. Does num2words support languages other than English?

Yes, num2words supports multiple languages, including but not limited to English.

Q4. Can I customize the output format of num2words?

Yes, num2words offers various customization options, including the ability to convert numbers into ordinal words and currency words.

Q5. Is num2words suitable for handling large numbers?

Yes, num2words can handle large numbers seamlessly, providing accurate word representations for numbers with multiple digits.

Q6. Are there alternatives to num2words for converting numbers to words?

Yes, there are alternative libraries and methods for converting numbers to words in Python, but num2words is widely used due to its simplicity and versatility.

Thatโ€™s a wrap!

I hope you enjoyed this article

Did you like it? Let me know in the comments below ๐Ÿ”ฅ and you can support me by buying me a coffee.

And donโ€™t forget to sign up to our email newsletter so you can get useful content like this sent right to your inbox!

Thanks!
Faraz ๐Ÿ˜Š

End of the article

Subscribe to my Newsletter

Get the latest posts delivered right to your inbox


Latest Post