开发者

converting str to int and vice versa

开发者 https://www.devze.com 2023-03-27 12:14 出处:网络
I have a need to convert a string to an int and back after doing some processing. And the string has to be in words like below:

I have a need to convert a string to an int and back after doing some processing. And the string has to be in words like below:

45 - forty five

99 - ninety nine

I s开发者_运维问答earched everywhere for a hint but couldn't find any. I know the obvious switch-case logic but I am curious if theres a more intelligent way doing it with less lines of code.


use num2words, see(https://www.geeksforgeeks.org/python-number-to-words-using-num2words/)

  from num2words import num2words
  def num2word(num):
     """
      convert numbers in a string to their word representation
     """  
     try:
         return num2words(num, lang='en_IN')
     except:
         return num


Have a look at http://pypi.python.org/pypi/PyNum2Word

0

精彩评论

暂无评论...
验证码 换一张
取 消