# Convert words to numbers and back?

**URL:** <https://www.ruby-forum.com/t/convert-words-to-numbers-and-back/121139>\
**Category:** Ruby\
**Created:** [November 24, 2007, 1:21pm UTC](https://www.ruby-forum.com/t/convert-words-to-numbers-and-back/121139 "2007-11-24T13:21:21Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![envygeeks](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/e68b1a/32.png) [@envygeeks](https://www.ruby-forum.com/u/envygeeks)\
**Post date:** [November 24, 2007, 1:21pm UTC](https://www.ruby-forum.com/t/convert-words-to-numbers-and-back/121139/1 "2007-11-24T13:21:21Z")

</div>

I was wondering if somebody could give me some insight and help on how  
to convert words to numbers and back again. Maybe a function?

---

<div class="post-metadata">

**Author:** ![envygeeks](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/e68b1a/32.png) [@envygeeks](https://www.ruby-forum.com/u/envygeeks)\
**Post date:** [November 24, 2007, 2:05pm UTC](https://www.ruby-forum.com/t/convert-words-to-numbers-and-back/121139/2 "2007-11-24T14:05:27Z")

</div>

Check [http://www.deveiate.org/projects/Linguistics/](http://www.deveiate.org/projects/Linguistics/)  
I think numwords is what you are looking for.

---

<div class="post-metadata">

**Author:** ![envygeeks](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/e68b1a/32.png) [@envygeeks](https://www.ruby-forum.com/u/envygeeks)\
**Post date:** [November 24, 2007, 2:11pm UTC](https://www.ruby-forum.com/t/convert-words-to-numbers-and-back/121139/3 "2007-11-24T14:11:22Z")

</div>

I don’t know exactly what you mean.  
Do you have something like: “123” ← a String which you want to conert  
into  
an integer? like 123 ?

if so, there are these functions:

str.to\_i(base=10) =\> integer

Returns the result of interpreting leading characters in str as an  
integer  
base base (2, 8, 10, or 16). Extraneous characters past the end of a  
valid  
number are ignored. If there is not a valid number at the start of str,  
0 is  
returned. This method never raises an exception.

“12345”.to\_i #=\> 12345  
“99 red balloons”.to\_i #=\> 99  
“0a”.to\_i #=\> 0  
“0a”.to\_i(16) #=\> 10  
“hello”.to\_i #=\> 0  
“1100101”.to\_i(2) #=\> 101  
“1100101”.to\_i(8) #=\> 294977  
“1100101”.to\_i(10) #=\> 1100101  
“1100101”.to\_i(16) #=\> 17826049  
orstr.to\_f =\> floatReturns the result of interpreting leading characters  
in  
str as a floating point number. Extraneous characters past the end of a  
valid number are ignored. If there is not a valid number at the start of  
str, 0.0 is returned. This method never raises an exception.  
“123.45e1”.to\_f #=\> 1234.5  
“45.67 degrees”.to\_f #=\> 45.67  
“thx1138”.to\_f #=\> 0.0  
bye sala----- Original Message -----  
From: “Andrei M.” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
To: “ruby-talk ML” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
Sent: Saturday, November 24, 2007 2:05 PM  
Subject: Re: Convert words to numbers and back?

---

<div class="post-metadata">

**Author:** ![envygeeks](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/e68b1a/32.png) [@envygeeks](https://www.ruby-forum.com/u/envygeeks)\
**Post date:** [November 24, 2007, 2:22pm UTC](https://www.ruby-forum.com/t/convert-words-to-numbers-and-back/121139/4 "2007-11-24T14:22:35Z")

</div>

I think the OP wants to know how he could turn 3 into “three” or 2001  
into  
“two thousand and one”.

---

<div class="post-metadata">

**Author:** ![envygeeks](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/e68b1a/32.png) [@envygeeks](https://www.ruby-forum.com/u/envygeeks)\
**Post date:** [November 24, 2007, 2:48pm UTC](https://www.ruby-forum.com/t/convert-words-to-numbers-and-back/121139/5 "2007-11-24T14:48:20Z")

</div>

2007/11/24, Andrei M. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> I think the OP wants to know how he could turn 3 into “three” or 2001 into  
> “two thousand and one”.

I could also imagine that the OP wants conversion of char sequences to  
a single number (see below).

Amazing: three replies and three different interpretations of the  
question:

1. convert a textual number description into a number object

2. convert a string representation of a number into a number object

3. encode a char sequence in a single number (reminds me faintly of  
Goedel numbers)

I’d say 1 and 3 can be matched to the OP’s question, while 2 clearly  
cannot with 1 having the highest likelyhood to match the actual  
requirements.

Jordon, what is it that you are looking for?

Kind regards

robert

---

<div class="post-metadata">

**Author:** ![envygeeks](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/e68b1a/32.png) [@envygeeks](https://www.ruby-forum.com/u/envygeeks)\
**Post date:** [November 24, 2007, 3:06pm UTC](https://www.ruby-forum.com/t/convert-words-to-numbers-and-back/121139/6 "2007-11-24T15:06:34Z")

</div>

Hm, I’m pretty sure I meant " turn 3 into “three” or “two thousand and  
one”  
into 2001". 🙂

---

<div class="post-metadata">

**Author:** ![envygeeks](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/e68b1a/32.png) [@envygeeks](https://www.ruby-forum.com/u/envygeeks)\
**Post date:** [November 24, 2007, 4:58pm UTC](https://www.ruby-forum.com/t/convert-words-to-numbers-and-back/121139/7 "2007-11-24T16:58:59Z")

</div>

2007/11/24, Andrei M. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> Hm, I’m pretty sure I meant " turn 3 into “three” or “two thousand and one”  
> into 2001". 🙂

You are not the OP.

> > Amazing: three replies and three different interpretations of the  
> > cannot with 1 having the highest likelyhood to match the actual  
> > requirements.
> > 
> > Jordon, what is it that you are looking for?

^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ see what I mean?

robert

---

<div class="post-metadata">

**Author:** ![envygeeks](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/e68b1a/32.png) [@envygeeks](https://www.ruby-forum.com/u/envygeeks)\
**Post date:** [November 24, 2007, 10:57pm UTC](https://www.ruby-forum.com/t/convert-words-to-numbers-and-back/121139/8 "2007-11-24T22:57:57Z")

</div>

On 11/24/07, Robert K. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> 2007/11/24, Andrei M. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):
> 
> > Hm, I’m pretty sure I meant " turn 3 into “three” or “two thousand and  
> > one”  
> > into 2001". 🙂
> 
> You are not the OP.

Gee, isn’t that a surprise.

---

<div class="post-metadata">

**Author:** ![envygeeks](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/e68b1a/32.png) [@envygeeks](https://www.ruby-forum.com/u/envygeeks)\
**Post date:** [November 25, 2007, 8:03am UTC](https://www.ruby-forum.com/t/convert-words-to-numbers-and-back/121139/9 "2007-11-25T08:03:09Z")

</div>

What I need to do is convert “22/11/2007” to  
“twenty-two/eleven/two-thousand-seven” and then convert it back to  
“22/11/2007”, what I will be doing is building a plugin for Mephisto to  
highjack my archive URIs to words over numbers and then converting it  
back to Mephisto when it comes back in. Out as word in as number. That  
way no major changes have to be made to the backend itself. It doesn’t  
have to add in the dashes since I can simply do that with split (" “)  
and join (”-"). I did look into Linguistics but it is a one way  
conversion, I need 2 way conversion. Thanks.

---

<div class="post-metadata">

**Author:** ![envygeeks](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/e68b1a/32.png) [@envygeeks](https://www.ruby-forum.com/u/envygeeks)\
**Post date:** [November 25, 2007, 5:05pm UTC](https://www.ruby-forum.com/t/convert-words-to-numbers-and-back/121139/10 "2007-11-25T17:05:56Z")

</div>

On Nov 25, 12:03 am, Jordon B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> What I need to do is convert “22/11/2007” to  
> “twenty-two/eleven/two-thousand-seven” and then convert it back to  
> “22/11/2007”, what I will be doing is building a plugin for Mephisto to  
> highjack my archive URIs to words over numbers and then converting it  
> back to Mephisto when it comes back in. Out as word in as number. That  
> way no major changes have to be made to the backend itself. It doesn’t  
> have to add in the dashes since I can simply do that with split (" “)  
> and join (”-"). I did look into Linguistics but it is a one way  
> conversion, I need 2 way conversion. Thanks.

For 7 → “seven” see:  
[http://www.rubyquiz.com/quiz25.html](http://www.rubyquiz.com/quiz25.html)

The reverse would make a fun problem to solve, too.  
Here, let me get you started:

ENGLISH\_VALUE = {}  
%w| zero one two three four five six seven eight nine ten eleven  
twelve thirteen fourteen fifteen sixteen seventeen eighteen  
nineteen |.each\_with\_index{ |word,i| ENGLISH\_VALUE[word] = i }

%w| zero ten twenty thirty forty fifty sixty seventy eighty  
ninety|.each\_with\_index{ |word,i| ENGLISH\_VALUE[word] = i_10 }  
ENGLISH\_VALUE[‘hundred’] = 100  
%w| one thousand million billion trillion|.each\_with\_index{ |word,i|  
ENGLISH\_VALUE[word] = 10\*\*(i_3)  
}

class Integer  
def self.from\_english( words )  
values = words.downcase.split( /\W+/ ).map{ |word|  
ENGLISH\_VALUE[word]  
}  
# Put your magic here  
end  
end

TESTS = {  
‘one’=\>1,  
‘seventy three’=\>73,  
‘ninety nine’=\>99,  
‘one hundred’=\>100,  
‘one hundred one’=\>101,  
‘one hundred twenty’=\>120,  
‘three hundred sixty four’=\>364,  
‘eight thousand five’=\>8\_005,  
‘forty-three thousand twelve’=\>43\_012,  
‘two billion one hundred thousand seventeen’=\>2\_000\_100\_117  
}

TESTS.each{ |word,expected\_value|  
actual\_value = Integer.from\_english( word )  
unless actual\_value == expected\_value  
warn "From ‘#{word}’, " \<\<  
"expected: #{expected\_value}, " \<\<  
“actual: #{actual\_value.inspect}”  
end  
}

#=\> From ‘forty-three thousand twelve’, expected: 43012, actual: [40,  
3, 1000, 12]  
#=\> From ‘one hundred one’, expected: 101, actual: [1, 100, 1]  
#=\> From ‘ninety nine’, expected: 99, actual: [90, 9]  
#=\> From ‘one’, expected: 1, actual: [1]  
#=\> From ‘eight thousand five’, expected: 8005, actual: [8, 1000, 5]  
#=\> From ‘one hundred twenty’, expected: 120, actual: [1, 100, 20]  
#=\> From ‘two billion one hundred thousand seventeen’, expected:  
2000100117, actual: [2, 1000000000, 1, 100, 1000, 17]  
#=\> From ‘three hundred sixty four’, expected: 364, actual: [3, 100,  
60, 4]  
#=\> From ‘one hundred’, expected: 100, actual: [1, 100]  
#=\> From ‘seventy three’, expected: 73, actual: [70, 3]
