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.
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.
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'
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
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.