conversions.convert_number_to_words

Classes

NumberWords

Create a collection of name/value pairs.

NumberingSystem

Create a collection of name/value pairs.

Functions

convert_number(→ str)

Converts an integer to English words.

convert_small_number(→ str)

Converts small, non-negative integers with irregular constructions in English (i.e.,

Module Contents

class conversions.convert_number_to_words.NumberWords(*args, **kwds)

Bases: enum.Enum

Create a collection of name/value pairs.

Example enumeration:

>>> class Color(Enum):
...     RED = 1
...     BLUE = 2
...     GREEN = 3

Access them by:

  • attribute access:

    >>> Color.RED
    <Color.RED: 1>
    
  • value lookup:

    >>> Color(1)
    <Color.RED: 1>
    
  • name lookup:

    >>> Color['RED']
    <Color.RED: 1>
    

Enumerations can be iterated over, and know how many members they have:

>>> len(Color)
3
>>> list(Color)
[<Color.RED: 1>, <Color.BLUE: 2>, <Color.GREEN: 3>]

Methods can be added to enumerations, and members can have their own attributes – see the documentation for details.

ONES: ClassVar[dict[int, str]]
TEENS: ClassVar[dict[int, str]]
TENS: ClassVar[dict[int, str]]
class conversions.convert_number_to_words.NumberingSystem(*args, **kwds)

Bases: enum.Enum

Create a collection of name/value pairs.

Example enumeration:

>>> class Color(Enum):
...     RED = 1
...     BLUE = 2
...     GREEN = 3

Access them by:

  • attribute access:

    >>> Color.RED
    <Color.RED: 1>
    
  • value lookup:

    >>> Color(1)
    <Color.RED: 1>
    
  • name lookup:

    >>> Color['RED']
    <Color.RED: 1>
    

Enumerations can be iterated over, and know how many members they have:

>>> len(Color)
3
>>> list(Color)
[<Color.RED: 1>, <Color.BLUE: 2>, <Color.GREEN: 3>]

Methods can be added to enumerations, and members can have their own attributes – see the documentation for details.

classmethod max_value(system: str) int

Gets the max value supported by the given number system.

>>> NumberingSystem.max_value("short") == 10**18 - 1
True
>>> NumberingSystem.max_value("long") == 10**21 - 1
True
>>> NumberingSystem.max_value("indian") == 10**19 - 1
True
INDIAN = ((14, 'crore crore'), (12, 'lakh crore'), (7, 'crore'), (5, 'lakh'), (3, 'thousand'), (2, 'hundred'))
LONG = ((15, 'billiard'), (9, 'milliard'), (6, 'million'), (3, 'thousand'), (2, 'hundred'))
SHORT = ((15, 'quadrillion'), (12, 'trillion'), (9, 'billion'), (6, 'million'), (3, 'thousand'), (2, 'hundred'))
conversions.convert_number_to_words.convert_number(num: int, system: Literal['short', 'long', 'indian'] = 'short') str

Converts an integer to English words.

Parameters:
  • num – The integer to be converted

  • system – The numbering system (short, long, or Indian)

>>> convert_number(0)
'zero'
>>> convert_number(1)
'one'
>>> convert_number(100)
'one hundred'
>>> convert_number(-100)
'negative one hundred'
>>> convert_number(123_456_789_012_345) 
'one hundred twenty-three trillion four hundred fifty-six billion
seven hundred eighty-nine million twelve thousand three hundred forty-five'
>>> convert_number(123_456_789_012_345, "long") 
'one hundred twenty-three thousand four hundred fifty-six milliard
seven hundred eighty-nine million twelve thousand three hundred forty-five'
>>> convert_number(12_34_56_78_90_12_345, "indian") 
'one crore crore twenty-three lakh crore
forty-five thousand six hundred seventy-eight crore
ninety lakh twelve thousand three hundred forty-five'
>>> convert_number(10**18)
Traceback (most recent call last):
...
ValueError: Input number is too large
>>> convert_number(10**21, "long")
Traceback (most recent call last):
...
ValueError: Input number is too large
>>> convert_number(10**19, "indian")
Traceback (most recent call last):
...
ValueError: Input number is too large
conversions.convert_number_to_words.convert_small_number(num: int) str

Converts small, non-negative integers with irregular constructions in English (i.e., numbers under 100) into words.

>>> convert_small_number(0)
'zero'
>>> convert_small_number(5)
'five'
>>> convert_small_number(10)
'ten'
>>> convert_small_number(15)
'fifteen'
>>> convert_small_number(20)
'twenty'
>>> convert_small_number(25)
'twenty-five'
>>> convert_small_number(-1)
Traceback (most recent call last):
...
ValueError: This function only accepts non-negative integers
>>> convert_small_number(123)
Traceback (most recent call last):
...
ValueError: This function only converts numbers less than 100