1

I need to convert integers to their English (string) representation in python. Example:

5 = five
20 = twenty
etc.

I know, I can use conditions, but I need to convert many integers, so I need the quickest way to achieve it.

poke
  • 369,085
  • 72
  • 557
  • 602
  • Have you tried anything for yourself yet? We usually help with some *existing* code. You need to show us what you have done so far. – Martijn Pieters Mar 01 '13 at 12:59

3 Answers3

6

pynum2word can do this for you:

>>> import num2word
>>> dir(num2word)
['__builtins__', '__doc__', '__file__', '__name__', '__package__',
'_lang', '_loc', '_locale', '_locdict', '_module', '_modules',
'n2w', 'n2wmod', 'to_card', 'to_ord', 'to_ordnum', 'to_year']
>>> num2word.to_card(1111)
'one thousand, one hundred and eleven'
msw
  • 42,753
  • 9
  • 87
  • 112
Asim Ihsan
  • 1,501
  • 8
  • 18
  • Very nice! +1 I was unaware of this. – Grijesh Chauhan Mar 01 '13 at 13:21
  • How do I install pynum2word? I tried pip, easy_install, and apt-get, none worked. I downloaded the file directly from your link. There's no setup.py. How do I get it to where I can use it? – Hoopdady Mar 01 '13 at 13:34
  • @Hoopdady: Yes, very annoyingly `pynum2word` is not available in the Cheeseshop, and in fact isn't even packaged as a `distutils` extension! Very strange! You'll need to download it, extract it, and use it manually. – Asim Ihsan Mar 01 '13 at 13:42
0

The problem is that there is probably no library that provides such a functionality

That way to do this is analyze the number, divide it into small numbers and use static lists of defined words this article can help you maybe

However if you want to use something different then English, it can get tricky and you'll need to modify the function above

Jan Vorcak
  • 19,261
  • 14
  • 54
  • 90
0

If I were you, I would try to find a list online of numbers and their corresponding english translation. A text file would probably be best. You would then convert that file into a dictionary in python (or save it to a db). Example {'1': 'one', '2': 'two'...}. From there, its easy. I think finding that existing list is the time consuming part, but there has to be one out there.

Neil Aggarwal
  • 511
  • 1
  • 10
  • 29