Word2Number

1.1 · active · verified Sat Apr 11

Word2Number is a Python module, currently at version 1.1, designed to convert number words (e.g., 'three hundred and forty two') into their corresponding numeric digits (342). It supports positive numbers up to the billions (999,999,999,999). The library has an irregular release cadence, with updates addressing specific issues and improvements.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to import and use the `w2n.word_to_num` function for converting number words, including decimals and hyphenated numbers. It also shows how to handle potential `ValueError` exceptions for invalid inputs, which is the behavior introduced in version 1.1.

from word2number import w2n

# Basic conversion
print(w2n.word_to_num("two million three thousand nine hundred and eighty four"))

# Handling decimals
print(w2n.word_to_num('two point three'))

# With hyphens
print(w2n.word_to_num('one hundred thirty-five'))

# Direct digits (supported but not primary use-case)
print(w2n.word_to_num('112'))

# Example of an invalid input (will raise ValueError in v1.1+)
try:
    print(w2n.word_to_num('blah'))
except ValueError as e:
    print(f"Error: {e}")

view raw JSON →