Search code examples
rubynlplinguistics

How do I convert words to their equivalent number?


If I have a positive integer written as a string, such as "three" or "forty nine", is there a simple way to convert this to an integer?

I'm happy using Linguistics to convert the other way, but I've never tried this way!


Solution

  • Take a look at the numbers_in_words GitHub project. It seems to provide the functionality you are looking for, at least for english number strings.

    From the project docs:

    require 'numbers_in_words'
    require 'numbers_in_words/duck_punch'
    
    112.in_words
    #=> one hundred and twelve
    
    "Seventy million, five-hundred and fifty six thousand point eight nine three".in_numbers
    #=> 70556000.893