Overview
The amount-to-words conversion utilities transform numeric monetary values into their Spanish written equivalents. This is a legal requirement for Venezuelan invoices, where amounts must be displayed both numerically and written out in words. Two functions are available:monto_a_letras()- Primary function with currency-aware formatting (USD/Bolívares)monto_a_letras_b()- Alternative function with digit-by-digit decimal conversion
monto_a_letras() Function
Purpose
Converts monetary amounts to Spanish words with proper currency handling for dollars and bolívares, including appropriate decimal terminology (centavos vs céntimos).Function Signature
main.py:51-71
Parameters
The monetary amount to convert. Can be:
- Float with decimals:
1234.56 - Integer:
1000 - String representation:
"1234.56"
The currency type, affecting decimal terminology and suffix:
"dolares"- Uses “centavos” for decimals, no currency suffix"bolivares"- Uses “céntimos” for decimals, adds “bolívares” suffix when no decimals
Return Value
Returns the amount written in Spanish words. Returns
None if conversion fails due to invalid input.Format patterns:- Dollars with decimals:
"[integer words] con [decimal words] centavos" - Dollars without decimals:
"[integer words]" - Bolívares with decimals:
"[integer words] con [decimal words] céntimos" - Bolívares without decimals:
"[integer words] bolívares"
Currency Handling
- Dólares (USD)
- Bolívares (BSD)
For dollars, the currency name is not included in the output string. This allows flexibility in invoice templates.
Decimal Handling
The function implements precise decimal rounding:Calculate Decimal Cents
Subtracts integer, multiplies by 100, and rounds:
(1234.56 - 1234) * 100 → 56| Original Amount | Integer Part | Decimal Part | Behavior |
|---|---|---|---|
1234.56 | 1234 | 56 | Includes “con cincuenta y seis” |
1234.00 | 1234 | 0 | No decimal words |
1234.004 | 1234 | 0 | Rounds down, no decimals |
1234.995 | 1234 | 100 | Rounds to 100 céntimos/centavos |
0.99 | 0 | 99 | ”cero con noventa y nueve” |
Examples
monto_a_letras_b() Function
Purpose
Alternative conversion function that uses digit-by-digit pronunciation for decimal parts, producing more literal translations (e.g., “punto cinco seis” instead of “cincuenta y seis”).Function Signature
main.py:36-49
Parameters
The amount to convert. Accepts same types as
monto_a_letras() but without currency parameter.Return Value
Returns the amount in Spanish with digit-by-digit decimal pronunciation:
- With decimals:
"[integer words] punto [digit words]" - Without decimals:
"[integer words]" - On error:
None
Decimal Conversion Approach
Unlikemonto_a_letras(), this function converts each decimal digit individually:
Comparison: monto_a_letras() vs monto_a_letras_b()
| Amount | monto_a_letras() (USD) | monto_a_letras_b() |
|---|---|---|
1234.56 | mil doscientos treinta y cuatro con cincuenta y seis centavos | mil doscientos treinta y cuatro punto cinco seis |
1234.5 | mil doscientos treinta y cuatro con cincuenta centavos | mil doscientos treinta y cuatro punto cinco |
1234.50 | mil doscientos treinta y cuatro con cincuenta centavos | mil doscientos treinta y cuatro punto cinco cero |
1000 | mil | mil |
0.99 | cero con noventa y nueve centavos | cero punto nueve nueve |
When to use
monto_a_letras_b():- Invoice systems that prefer literal decimal pronunciation
- Used for Bolívares amounts in the invoice “MontoEnLetras” field (main.py:149)
- Simpler output without currency-specific logic
Examples
Spanish Language Conversion
Both functions use thenum2words library with Spanish language support:
Number Translation Examples
| Number | Spanish Words | Notes |
|---|---|---|
| 0 | cero | |
| 1 | uno | |
| 15 | quince | |
| 56 | cincuenta y seis | |
| 100 | cien | |
| 101 | ciento uno | ”ciento” for 101-199 |
| 1000 | mil | |
| 1234 | mil doscientos treinta y cuatro | |
| 1000000 | un millón |
The
num2words library handles all Spanish grammar rules including gender agreement, conjunctions (“y”), and special cases like “cien” vs “ciento”.Integration in Invoice Processing
Both functions are used in thetransformar_fila() pipeline:
Invoice JSON Output
Error Handling
Both functions include try-except blocks for robust error handling:Invalid Input Types
Non-numeric strings, None values, or objects that can’t be converted to float return
NoneConversion Failures
If
num2words fails or string operations error out, returns None gracefullyUnknown Currency
monto_a_letras() returns "Moneda no reconocida." for unsupported currency typesNull Safety
Both functions handle None, empty strings, and invalid numeric formats without crashing
Best Practices
Choose the Right Function
Choose the Right Function
- Use
monto_a_letras()when you need proper currency terminology and natural decimal reading - Use
monto_a_letras_b()when you need literal decimal digit pronunciation - For multi-currency invoices, use
monto_a_letras()with the appropriate currency parameter
Handle Decimal Precision
Handle Decimal Precision
- Always round amounts to 2 decimal places before conversion for consistent results
- Be aware that
monto_a_letras()automatically rounds decimals to cents monto_a_letras_b()preserves trailing zeros in the string representation
Validate Currency Codes
Validate Currency Codes
- Only use “dolares” or “bolivares” (lowercase preferred) with
monto_a_letras() - Implement fallback logic if the currency might be unsupported
- Consider adding validation before calling the function
Error Response Handling
Error Response Handling
- Always check for
Nonereturn values before using the result - Log conversion failures for debugging invoice processing issues
- Provide fallback text (e.g., “[Monto inválido]”) in invoice templates
Dependencies
num2words
Python library for converting numbers to words in multiple languages.Version: Compatible with num2words >= 0.5.12Documentation: https://github.com/savoirfairelinux/num2words
The
num2words library supports 40+ languages. This implementation specifically uses lang='es' for Spanish (Spain and Latin America) conventions.