Num2Words is a Python library designed to convert numerical values into their word representations across multiple languages. It supports various output formats, including cardinal numbers (e.g., 'forty-two'), ordinal numbers (e.g., 'forty-second'), years, and currency representations with appropriate localization. The library is currently active, with the latest stable version being 0.5.14, and receives regular updates, primarily focusing on adding new language support and fixing existing localizations.
pip install num2wordsVerified import paths — ran on the pinned version, not inferred.
This quickstart demonstrates how to import the `num2words` function and use it for various conversion types, including cardinal, ordinal, ordinal number, and currency formats, as well as specifying a target language.
Replace `ordinal=True` with `to='ordinal'` or `to='ordinal_num'`.
Ensure you are using a safe version (e.g., 0.5.14 or earlier safe versions). If you installed 0.5.15 or 0.5.16, downgrade immediately and audit your systems for compromise.
Implement a fallback mechanism, such as a `try-except NotImplementedError` block, to default to English or another supported language if the requested language is unavailable.
Review the output for critical applications, especially when using less common languages or complex number formats. Check GitHub issues for known language-specific problems.
Install the library using pip: `pip install num2words` If you have multiple Python versions, you might need to use `pip3 install num2words` or ensure you activate the correct virtual environment before installation.
You need to access the `num2words` function from within the imported module, or import the function directly: Option 1 (recommended): Import the function directly: `from num2words import num2words word_representation = num2words(42)` Option 2: Access the function through the module: `import num2words word_representation = num2words.num2words(42)`
Ensure that the value you pass to `num2words()` is an integer or a float. If you have a string representation of a number, convert it first: `number_str = '123' word_representation = num2words(int(number_str))` Or for a float: `float_str = '123.45' word_representation = num2words(float(float_str))`
Check the official `num2words` documentation or GitHub repository for the list of currently supported language codes. If the desired language is not supported, you may need to fallback to a supported language or contribute the missing language.
Example of handling:
`try:
word = num2words(100, lang='unsupported_lang_code')
except NotImplementedError:
word = num2words(100, lang='en') # Fallback to English`