Use the w2n.word_to_num method to convert natural language number words (e.g., "twenty one") into numeric digits (e.g., 21).
Supported Range: Positive numbers up to 999,999,999,999 (billions).
Capabilities:
- Converts full number words to integers.
- Supports decimal points (e.g., "two point three" $\rightarrow$
2.3). - Supports leading decimal points (e.g., "point one" $\rightarrow$
0.1). - Handles hyphenated numbers (e.g., "one hundred thirty-five" $\rightarrow$
135). - Accepts numeric strings directly (e.g., "112" $\rightarrow$
112).
Error Handling:
If the input is invalid, the method prints an error message to the console and returns None.
- Redundant numbers: e.g., "million million" triggers
Error: Redundant number! Please enter a valid number word... - Invalid words: e.g., "blah" triggers
Error: No valid number words found! Please enter a valid number word...
from word2number import w2n
# Basic conversion
print(w2n.word_to_num("two million three thousand nine hundred and eighty four")) # 2003984
# Decimal conversion
print(w2n.word_to_num('two point three')) # 2.3
print(w2n.word_to_num('point one')) # 0.1
# Numeric string conversion
print(w2n.word_to_num('112')) # 112
# Hyphenated numbers
print(w2n.word_to_num('one hundred thirty-five')) # 135