Mathwizurd.com is created by David Witten, a mathematics and computer science student at Stanford University. For more information, see the "About" page.

Turning Numbers into Words

An important and needed feature in Python is converting numbers into words. So, I set out to help millions, and thus created a program to do so. 

The first thing I did was create a dictionary from important numbers from one to one hundred. 

main_dict = {0:"",1:"one",2:"two", 3: "three",4:"four", 5:"five", 6:"six", 7:'seven', 8:'eight', 9:'nine', 10:'ten', 11:"eleven",12:"twelve",13:'thirteen',14:'fourteen',15:'fifteen',16:'sixteen',17:'seventeen',18:'eighteen',19:'nineteen',20:'twenty',30:'thirty',40:'forty',50:'fifty',60:'sixty',70:'seventy',80:'eighty',90:'ninety',100:'hundred'}

Basically, what i was going to do was split it up until trillions,  billions, millions, thousands, and ones.  So, 1,000,400,234 would become [0,0,1,0,400,234] (0 quadrillions, 0 trillions, 1 billion, 0 millions, 400 thousands, 234 ones). This is the code:

things = [(number//(10 ** i))%1000 for i in range(15, -3, -3)]

Later, I threw out each one that was zero, and parsed the rest, that's what "cent()" does. 

things = [(cent(things[i],main_dict) + " " + ['quadrillion','trillion','billion','million','thousand',''][i]).strip() for i in range(len(things)) if things[i] != 0]

cent parses numbers from 1 to 999. This the code for that function:

def cent(number,main_dict):
    array, length = [],len(str(number))
    g1 = [i for i in [number//100 * 100, number%100] if i != 0]
    g2 = [(number//(10 ** i) % 10) * (10 ** i) for i in range(2, -1,-1)]
    g = g1 if g1[-1] in main_dict else g2
    for n,i in enumerate(g):
        if (n == 0 and length == 3) or (i not in main_dict):
            array.append(main_dict[int(str(i)[0])])
        array.append(main_dict[i] if i in main_dict else main_dict[10 ** (length - 1)])
    return main_dict[number] if number in main_dict.keys() else ' '.join(array)

This creates two lists: one that splits numbers for each place (e.g. 823 = [800, 20, 3]) (g2), and g1, which makes it into the hundreds and the rest (e.g. [800, 23]), so if the last two digits were special, such as 13, it would immediately parse it. 

So, it parses the hundreds, and adds them to their power of ten (million, billion, etcetera). Basically, that's the entire algorithm. 

Here's the code: https://github.com/dwizard/num2word

David Witten

Matrix Operations in Python

Solving Linear Systems of Equations