[Tutor] Text numerals?

Danny Yoo dyoo at hkn.eecs.berkeley.edu
Sun May 16 04:42:02 EDT 2004



On Sun, 16 May 2004, Don Arnold wrote:

> > Is there a function that will return 'one' for 1, 'two' for two, etc.?
> >
>
>   I assume you mean 'two' for 2 ;).  But no, AFAIK there is no stdlib
> implementation.
>
> <snip dictionary suggestions>
>
>   The problem of converting full-blown numbers of any length into
> letters is much harder.  Some years ago I remember this being coded in
> python; if the author has a website, then Google is probably your best
> bet.


This problem comes up quite often.  It even showed its face in PyCon 2004:

    http://www.python.org/pycon/dc2004/papers/42/ex1-C/num2eng.py

There's also a project in SourceForge that handles the problem with
impressive generality:

    http://sourceforge.net/projects/pynum2word/

Converting numbers to words is a classic programming exercise.  As Don
shows, it's also a good way to practice using loops, dictionaries, and
string manipulation.




More information about the Tutor mailing list