### Install num2words from source Source: https://github.com/savoirfairelinux/num2words/blob/master/README.rst This command installs the num2words library by building it from the source code. This method requires downloading the source package and running the setup script. ```bash python setup.py install ``` -------------------------------- ### Command line usage of num2words Source: https://github.com/savoirfairelinux/num2words/blob/master/README.rst Shows examples of using the num2words library directly from the command line to convert numbers to words. It includes examples for cardinal numbers, numbers with decimals, and specifying the language. ```bash $ num2words 10001 ten thousand and one $ num2words 24,120.10 twenty-four thousand, one hundred and twenty point one $ num2words 24,120.10 -l es veinticuatro mil ciento veinte punto uno ``` -------------------------------- ### Install num2words using pip Source: https://github.com/savoirfairelinux/num2words/blob/master/README.rst This command installs the num2words library using pip, the Python package installer. Ensure you have pip installed and configured correctly. ```bash pip install num2words ``` -------------------------------- ### Num2words Usage Examples Source: https://github.com/savoirfairelinux/num2words/blob/master/README.rst Demonstrates how to use the num2words library for number-to-word conversion in Python code and via the command line. ```APIDOC ## Num2words Library Usage ### Description This section provides examples of how to use the `num2words` library for converting numbers to words. ### Installation Install using pip: ```bash pip install num2words ``` ### Command Line Usage Convert a number to words: ```bash $ num2words 10001 ten thousand and one ``` Convert a number with decimals and specify language: ```bash $ num2words 24,120.10 -l es veinticuatro mil ciento veinte punto uno ``` Convert to currency format: ```bash $ num2words 2.14 -l es --to currency dos euros con catorce céntimos ``` ### Python Code Usage Import the function and use it: ```python from num2words import num2words # Cardinal number (default) print(num2words(42)) # Output: forty-two # Ordinal number print(num2words(42, to='ordinal')) # Output: forty-second # Specify language print(num2words(42, lang='fr')) # Output: quarante-deux ``` ### Parameters The `num2words` function accepts the following main arguments: #### Arguments - **number** (int or float) - The number to convert. - **to** (string) - The type of conversion. Supported values: - `cardinal` (default) - `ordinal` - `ordinal_num` - `year` - `currency` - **lang** (string) - The language code for conversion. Supported values include: - `en` (English, default) - `es` (Spanish) - `fr` (French) - `de` (German) - and many others (see full list in documentation). If an unsupported language is provided, a `NotImplementedError` is raised. You can handle this with a fallback to a default language: ```python try: result = num2words(123, lang=mylang) except NotImplementedError: result = num2words(123, lang='en') ``` ### Supported Languages The library supports a wide range of languages, including: - `en` (English) - `es` (Spanish) - `fr` (French) - `de` (German) - `it` (Italian) - `ja` (Japanese) - `ko` (Korean) - `ru` (Russian) - `zh` (Chinese) - and many more. For a complete list, refer to the library's documentation or source code. ``` -------------------------------- ### Convert number to words in French using num2words Source: https://github.com/savoirfairelinux/num2words/blob/master/README.rst Illustrates how to specify a different language for number-to-word conversion. This example converts the number 42 into French words. ```python from num2words import num2words print(num2words(42, lang='fr')) ``` -------------------------------- ### Run num2words CI test suite Source: https://github.com/savoirfairelinux/num2words/blob/master/README.rst This command executes the full Continuous Integration (CI) test suite for the num2words library, which includes linting and testing across multiple Python environments. It requires tox to be installed. ```bash pip install tox tox ``` -------------------------------- ### Handle unsupported languages in num2words Source: https://github.com/savoirfairelinux/num2words/blob/master/README.rst Provides an example of how to gracefully handle cases where an unsupported language is provided to the num2words function. It uses a try-except block to fall back to English if the specified language is not implemented. ```python from num2words import num2words try: return num2words(42, lang=mylang) except NotImplementedError: return num2words(42, lang='en') ``` -------------------------------- ### Error Handling in Num2words (Python) Source: https://context7.com/savoirfairelinux/num2words/llms.txt Provides examples of how to handle common exceptions raised by the num2words library, such as unsupported languages, overflow errors for extremely large numbers, invalid ordinal inputs (floats, negatives), and unsupported currency codes. ```python from num2words import num2words # Handle unsupported language try: result = num2words(42, lang='xyz') except NotImplementedError: result = num2words(42, lang='en') # Fallback to English print(f"Language not supported, using English: {result}") # Output: Language not supported, using English: forty-two # Handle very large numbers (overflow) try: result = num2words(10**400) # Too large except OverflowError as e: print(f"Number too large: {e}") # Output: Number too large: abs(...) must be less than ... # Handle invalid ordinal (float) try: result = num2words(3.14, to='ordinal') except TypeError as e: print(f"Invalid ordinal: {e}") # Output: Invalid ordinal: Cannot treat float 3.14 as ordinal. # Handle invalid ordinal (negative) try: result = num2words(-5, to='ordinal') except TypeError as e: print(f"Invalid ordinal: {e}") # Output: Invalid ordinal: Cannot treat negative num -5 as ordinal. # Handle unsupported currency try: result = num2words(100, to='currency', currency='XYZ') except NotImplementedError as e: print(f"Currency error: {e}") ``` -------------------------------- ### Code Validation Commands (Shell) Source: https://github.com/savoirfairelinux/num2words/blob/master/num2words/README.md Commands to validate the code quality and test coverage for new language contributions. This involves installing dependencies, running tox for formatting and linting, and using coverage for report generation. ```Shell pip install -r requirements-test.txt tox python3 -m coverage report -m ``` -------------------------------- ### Num2words Command Line Interface Usage Source: https://context7.com/savoirfairelinux/num2words/llms.txt Illustrates how to use the num2words utility directly from the command line for various number-to-word conversions, including basic, floating-point, different languages, currency, ordinal, and year formats. Also shows how to list supported languages and converters. ```bash # Basic conversion $ num2words 10001 # Output: ten thousand and one # Floating point numbers $ num2words 24,120.10 # Output: twenty-four thousand, one hundred and twenty point one # Different language $ num2words 24,120.10 -l es # Output: veinticuatro mil ciento veinte punto uno # Currency conversion $ num2words 2.14 -l es --to currency # Output: dos euros con catorce céntimos # Ordinal conversion $ num2words 42 --to ordinal # Output: forty-second # Year conversion $ num2words 2024 --to year # Output: twenty twenty-four # List all supported languages $ num2words --list-languages # Output: am ar az be ... # List all converters $ num2words --list-converters # Output: cardinal currency ordinal ordinal_num year ``` -------------------------------- ### Convert Numbers to Currency Format (Python) Source: https://context7.com/savoirfairelinux/num2words/llms.txt Shows how to convert numbers into their spoken currency format using num2words. Supports various currencies (USD, EUR, GBP, MXN, JPY, KRW, INR), custom separators, optional cents, adjectives, and different languages. ```python from num2words import num2words # US Dollars print(num2words(38.40, to='currency', currency='USD')) # Output: thirty-eight dollars, forty cents print(num2words(1.01, to='currency', currency='USD', separator=' and', cents=True)) # Output: one dollar and one cent print(num2words(0, to='currency', currency='USD')) # Output: zero dollars # With adjective prefix print(num2words(4778.00, to='currency', currency='USD', adjective=True, cents=True, separator=' and')) # Output: four thousand, seven hundred and seventy-eight US dollars and zero cents # Euros print(num2words(25.50, to='currency', currency='EUR')) # Output: twenty-five euro, fifty cents # British Pounds print(num2words(100.01, to='currency', currency='GBP', separator=' and')) # Output: one hundred pounds sterling and one penny # Mexican Pesos print(num2words(158.30, to='currency', currency='MXN', cents=True, separator=' and')) # Output: one hundred and fifty-eight pesos and thirty cents # Japanese Yen print(num2words(2000.00, to='currency', currency='JPY', cents=True, separator=' and')) # Output: two thousand yen and zero sen # Korean Won print(num2words(5000, to='currency', currency='KRW')) # Output: five thousand won # Indian Rupees print(num2words(1500.75, to='currency', currency='INR')) # Output: one thousand, five hundred rupees, seventy-five paise # Numeric cents (cents=False) print(num2words(38.40, to='currency', currency='USD', separator=' and', cents=False)) # Output: thirty-eight dollars and 40 cents # Currency in other languages print(num2words(2.14, lang='es', to='currency')) # Output: dos euros con catorce céntimos print(num2words(100.50, lang='fr', to='currency', currency='EUR')) # Output: cent euros, cinquante centimes print(num2words(250.99, lang='de', to='currency', currency='EUR')) # Output: zweihundertfünfzig Euro, neunundneunzig Cent ``` -------------------------------- ### Convert Numbers to Years in Words (Python) Source: https://context7.com/savoirfairelinux/num2words/llms.txt Demonstrates how to convert integers into their year-based word representation using the num2words library. Supports standard years, years ending in '00', years with 'oh' pronunciation, BC years, and custom suffixes. ```python from num2words import num2words # Standard year conversion print(num2words(2017, to='year')) # Output: twenty seventeen print(num2words(1865, to='year')) # Output: eighteen sixty-five # Years ending in 00 print(num2words(1900, to='year')) # Output: nineteen hundred print(num2words(2000, to='year')) # Output: two thousand # Years with "oh" (01-09) print(num2words(1901, to='year')) # Output: nineteen oh-one print(num2words(2001, to='year')) # Output: two thousand and one print(num2words(905, to='year')) # Output: nine oh-five # BC years (negative numbers) print(num2words(-44, to='year')) # Output: forty-four BC # Custom suffix print(num2words(-44, to='year', suffix='BCE')) # Output: forty-four BCE print(num2words(1, to='year', suffix='AD')) # Output: one AD print(num2words(66, to='year', suffix='m.y.a.')) # Output: sixty-six m.y.a. # Large years print(num2words(-66000000, to='year')) # Output: sixty-six million BC ``` -------------------------------- ### Convert number to currency words in Spanish Source: https://github.com/savoirfairelinux/num2words/blob/master/README.rst Demonstrates converting a number to its currency representation in Spanish, including cents. This requires specifying the language and the 'currency' type. ```python from num2words import num2words print(num2words(2.14, lang='es', to='currency')) ``` -------------------------------- ### New Language Class Template (Python) Source: https://github.com/savoirfairelinux/num2words/blob/master/num2words/README.md A template for creating a new language class in num2words. It must inherit from Num2Word_EU and implement `to_cardinal` and `to_ordinal` methods. Optional parameters like gender and formality can be added. ```Python from .lang_EU import Num2Word_EU class Num2Word_CY(Num2Word_EU): def setup(self): Num2Word_EU.setup(self) def __init__(self): pass def to_ordinal(self, number): # implement here your code. number is the integer to be transformed into an ordinal # as a word (str) # which is returned return "NOT IMPLEMENTED" def to_cardinal(self, number): # implement here your code. number is the integer to be transformed into an cardinal # as a word (str) # which is returned return "NOT IMPLEMENTED" ``` -------------------------------- ### Convert number to words in Python Source: https://github.com/savoirfairelinux/num2words/blob/master/README.rst Demonstrates the basic usage of the num2words function in Python to convert a number into its word representation. The default language is English. ```python from num2words import num2words print(num2words(42)) ``` -------------------------------- ### Convert number to ordinal words in Python Source: https://github.com/savoirfairelinux/num2words/blob/master/README.rst Shows how to convert a number into its ordinal word form using the 'ordinal' type in the num2words function. This is useful for generating text like 'first', 'second', etc. ```python from num2words import num2words print(num2words(42, to='ordinal')) ``` -------------------------------- ### Convert numbers with language localization Source: https://context7.com/savoirfairelinux/num2words/llms.txt Use the 'lang' parameter to specify the target language for conversion. The library supports ISO language codes and regional variants, with automatic fallback mechanisms. ```python from num2words import num2words print(num2words(42, lang='en')) print(num2words(42, lang='fr')) print(num2words(1523, lang='es')) print(num2words(99, lang='de')) print(num2words(1000, lang='ru')) print(num2words(12345, lang='zh_CN')) print(num2words(2024, lang='ja')) print(num2words(100, lang='ar')) print(num2words(50, lang='hi')) print(num2words(70, lang='fr_BE')) print(num2words(70, lang='fr_CH')) ``` -------------------------------- ### Retrieve Supported Languages in num2words Source: https://context7.com/savoirfairelinux/num2words/llms.txt This snippet demonstrates how to programmatically retrieve and list all supported language codes available in the num2words library by accessing the CONVERTER_CLASSES dictionary. ```python from num2words import CONVERTER_CLASSES # Get all supported languages supported_languages = sorted(CONVERTER_CLASSES.keys()) print(supported_languages) ``` -------------------------------- ### Convert numbers to year format Source: https://context7.com/savoirfairelinux/num2words/llms.txt Convert numbers to a year representation using the 'year' option. This handles specific year-based formatting conventions. ```python from num2words import num2words print(num2words(1990, to='year')) print(num2words(2017, to='year')) ``` -------------------------------- ### Convert to ordinal numerals Source: https://context7.com/savoirfairelinux/num2words/llms.txt Convert numbers to ordinal numeral format (e.g., 1st, 2nd) using the 'ordinal_num' option. This returns the number combined with the appropriate suffix. ```python from num2words import num2words print(num2words(1, to='ordinal_num')) print(num2words(2, to='ordinal_num')) print(num2words(21, to='ordinal_num')) print(num2words(111, to='ordinal_num')) ``` -------------------------------- ### Convert numbers to cardinal words Source: https://context7.com/savoirfairelinux/num2words/llms.txt The primary num2words() function converts integers, floats, and numeric strings into their cardinal word representations. It handles negative values and floating-point precision automatically. ```python from num2words import num2words print(num2words(42)) print(num2words(1234567)) print(num2words(-15)) print(num2words(12.53)) print(num2words("24,120.10")) ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.