HiveBrain v1.2.0
Get Started
← Back to all entries
patternpythonMinor

Numbers to English Strings in Python3

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
numbersstringsenglishpython3

Problem

Inspired by the recent surge of questions dealing with changing numbers to their English equivalents (See here, here, and here), I decided to write my own version in Python.

zero_to_nineteen = (
    "zero",
    "one",
    "two",
    "three",
    "four",
    "five",
    "six",
    "seven",
    "eight",
    "nine",
    "ten",
    "eleven",
    "twelve",
    "thirteen",
    "fourteen" ,
    "fifteen",
    "sixteen",
    "seventeen",
    "eighteen",
    "nineteen"
)

tens = (
    "zero",
    "ten",
    "twenty",
    "thirty",
    "forty",
    "fifty",
    "sixty",
    "seventy",
    "eighty",
    "ninety"
)

suffixes = {
    3 : "thousand",
    6 : "million",
    9 : "billion",
    12 : "trillion",
    15 : "quadrillion",
    18 : "quintillion",
    21 : "sextillion",
    24 : "septillion",
    27 : "octillion",
    30 : "nonillion",
    33 : "decillion",
    36 : "undecillion"
    39 : "duodecillion",
    42 : "tredicillion",
    45 : "quattuordecillion",
    48 : "quinquadecillion",
    51 : "sedecillion",
    54 : "septendecillion",
    57 : "octodecillion",
    60 : "novendecillion",
    63 : "vigintillion",
    66 : "unvigintillion",
    69 : "duovigintillion",
    72 : "tresvigintillion",
    75 : "quattuorvigintillion",
    78 : "quinquavigintillion",
    81 : "sesvigintillion",
    84 : "septemvigintillion",
    87 : "octovigintillion",
    90 : "novemvigintillion",
    93 : "trigintillion",
    96 : "untrigintillion",
    99 : "duotrigintillion",
    102 : "trestrigintilion",
    105 : "quattuortrigintillion",
    108 : "quinquatrigintillion",
    111 : "sestrigintillion",
    114 : "septentrigintillion",
    117 : "octotrigintillion",
    120 : "noventrigintillion",
    123 : "quadragintillion"
}

def spell_out(number):
    """Returns a string representation of the number, in english"""
    if isinstance(number, float):
        raise ValueError("number must be an integer")

    if number  9:
        answer += 1
        number //= 10
    return answer

Solution

It looks mostly fine. I only have three comments:

-
The definitions of zero_to_nineteen and tens is too long for my taste. It will look more readable like this::

tens = ("zero", "ten", "twenty", "thirty", "forty",
        "fifty", "sixty", "seventy", "eighty", "ninety")


-
Reject floats straight away is too much. One cannot then ask for the spelling of 1e6. I would do:

if isinstance(number, float):
if number % 1 == 0:
number = int(number)
else:
raise ValueError("number must be an integer")

elif not isinstance(number, int):
raise ValueError("number must be a number")


Note that your function would happily accept things like 1j, "abc", None...

-
For Guido's beard, don't roll your own log function! use int(math.log10)

Code Snippets

tens = ("zero", "ten", "twenty", "thirty", "forty",
        "fifty", "sixty", "seventy", "eighty", "ninety")

Context

StackExchange Code Review Q#59447, answer score: 4

Revisions (0)

No revisions yet.