-2

I’m trying to be user-friendly and output some integers in written English. For example, if the number were 1 I would like it to come out 'one', or if it were 42 it would return 'forty two'. Before I code my own solution, has anyone seen a library that does that? None of my searches have returned any results, even though it seems like someone must have needed to do this at some time. I’m using Python 3.6, if that makes a difference.

Curtis Pew
  • 107
  • 1
  • 7
  • Sorry, no library recommendations here. If you try something and stumble while on it, feel free to bring your problems here :) – Alfabravo Mar 27 '18 at 21:56
  • https://pypi.python.org/pypi/num2words/0.5.6 – Nanda Mar 27 '18 at 21:56
  • One google search later... https://imgur.com/a/SrJyz – Luke Mar 27 '18 at 21:56
  • 1
    Possible duplicate of [How do I tell Python to convert integers into words](https://stackoverflow.com/questions/8982163/how-do-i-tell-python-to-convert-integers-into-words) – Nanda Mar 27 '18 at 21:58
  • num2words was just what I was looking for. I tried searching here and using DuckDuckGo but I guess I used the wrong keywords. Thanks. – Curtis Pew Mar 27 '18 at 22:03

2 Answers2

2

The inflect package can do this.

https://pypi.python.org/pypi/inflect

$ pip install inflect

and then:

>>>import inflect
>>>p = inflect.engine()
>>>p.number_to_words(99)
ninety-nine

Exact answer taken from here

Austin A
  • 566
  • 2
  • 15
0

here is my approach to do this.

>>> import num2word
>>> num2word.to_card(5)
    'five'
>>> num2word.to_card(45)
    'forty-five'
>>> num2word.to_card(1252)
    'one thousand, two hundred and fifty-two'

hope this helps.

toheedNiaz
  • 1,435
  • 1
  • 10
  • 14