Is there a way to convert number words to Integers?

前端 未结 16 2284
北恋
北恋 2020-11-22 06:14

I need to convert one into 1, two into 2 and so on.

Is there a way to do this with a library or a class or anythi

16条回答
  •  故里飘歌
    2020-11-22 07:03

    There's a ruby gem by Marc Burns that does it. I recently forked it to add support for years. You can call ruby code from python.

      require 'numbers_in_words'
      require 'numbers_in_words/duck_punch'
    
      nums = ["fifteen sixteen", "eighty five sixteen",  "nineteen ninety six",
              "one hundred and seventy nine", "thirteen hundred", "nine thousand two hundred and ninety seven"]
      nums.each {|n| p n; p n.in_numbers}
    

    results:
    "fifteen sixteen" 1516 "eighty five sixteen" 8516 "nineteen ninety six" 1996 "one hundred and seventy nine" 179 "thirteen hundred" 1300 "nine thousand two hundred and ninety seven" 9297

提交回复
热议问题