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!

有帮助吗?

解决方案

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
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top