# Format numbers as words

**URL:** <https://www.ruby-forum.com/t/format-numbers-as-words/59173>\
**Category:** Rails\
**Created:** [April 30, 2006, 3:24am UTC](https://www.ruby-forum.com/t/format-numbers-as-words/59173 "2006-04-30T03:24:43Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![idlefingers](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/898d66/32.png) [@idlefingers](https://www.ruby-forum.com/u/idlefingers)\
**Post date:** [April 30, 2006, 3:24am UTC](https://www.ruby-forum.com/t/format-numbers-as-words/59173/1 "2006-04-30T03:24:43Z")

</div>

Does anyone know if there is a function available which can format any  
entered number as words?

eg: ‘1234’ would be ‘One Thousand Two Hundred and Thirty Four’

In the past, I would have said this is a tall order, but from what I’ve  
seen with playing with rails for a short time, I don’t know what to  
expect. 🙂

Thanks for any help,  
Damien

---

<div class="post-metadata">

**Author:** ![idlefingers](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/898d66/32.png) [@idlefingers](https://www.ruby-forum.com/u/idlefingers)\
**Post date:** [April 30, 2006, 11:39am UTC](https://www.ruby-forum.com/t/format-numbers-as-words/59173/2 "2006-04-30T11:39:44Z")

</div>

Damien T. wrote:

> Does anyone know if there is a function available which can format any  
> entered number as words?
> 
> eg: ‘1234’ would be ‘One Thousand Two Hundred and Thirty Four’
> 
> In the past, I would have said this is a tall order, but from what I’ve  
> seen with playing with rails for a short time, I don’t know what to  
> expect. 🙂
> 
> Thanks for any help,  
> Damien  
> [Ruby Quiz - English Numerals (#25)](http://www.rubyquiz.com/quiz25.html)

j`ey  
[http://www.eachmapinject.com](http://www.eachmapinject.com)

---

<div class="post-metadata">

**Author:** ![idlefingers](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/898d66/32.png) [@idlefingers](https://www.ruby-forum.com/u/idlefingers)\
**Post date:** [May 1, 2006, 2:29am UTC](https://www.ruby-forum.com/t/format-numbers-as-words/59173/3 "2006-05-01T02:29:18Z")

</div>

joey\_\_ wrote:

> Damien T. wrote:
> 
> > Does anyone know if there is a function available which can format any  
> > entered number as words?
> > 
> > eg: ‘1234’ would be ‘One Thousand Two Hundred and Thirty Four’
> > 
> > In the past, I would have said this is a tall order, but from what I’ve  
> > seen with playing with rails for a short time, I don’t know what to  
> > expect. 🙂
> > 
> > Thanks for any help,  
> > Damien  
> > [Ruby Quiz - English Numerals (#25)](http://www.rubyquiz.com/quiz25.html)
> 
> j`ey  
> [http://www.eachmapinject.com](http://www.eachmapinject.com)

Thanks, that looks perfect, but i’m really confused as to how I  
implement it… it doesnt explain on that page how to actually use it??

Sorry, i’m a real noob with rails.

---

<div class="post-metadata">

**Author:** ![idlefingers](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/898d66/32.png) [@idlefingers](https://www.ruby-forum.com/u/idlefingers)\
**Post date:** [May 1, 2006, 1:07pm UTC](https://www.ruby-forum.com/t/format-numbers-as-words/59173/4 "2006-05-01T13:07:51Z")

</div>

> > Thanks, that looks perfect, but i’m really confused as to how I  
> > implement it… it doesnt explain on that page how to actually use it??
> > 
> > Sorry, i’m a real noob with rails.
> 
> Add the code from:  
> [http://www.ruby-talk.org/cgi-bin/scat.rb/ruby/ruby-talk/135735](http://www.ruby-talk.org/cgi-bin/scat.rb/ruby/ruby-talk/135735)
> 
> Into a english\_numbers.rb in the lib/ folder. Then do require  
> ‘lib/english\_numbers.rb’
> 
> Then do EnglishNumerals.to\_English(number)
> 
> j`ey  
> [http://eachmapinject.com](http://eachmapinject.com)

Thanks for the help, but i’m still quite confused.

I did as you said, made the file in lib but I wasn’t too sure where to  
put the require so I’ve tried everything - in the model, helper and  
controller. Wherever I put it, it returned an error, but when it was in  
the controller, rather than saying it couldn’t find the file or  
something to that effect, it said “wrong number of arguments (0 for 1)”

---

<div class="post-metadata">

**Author:** ![idlefingers](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/898d66/32.png) [@idlefingers](https://www.ruby-forum.com/u/idlefingers)\
**Post date:** [May 3, 2006, 6:56pm UTC](https://www.ruby-forum.com/t/format-numbers-as-words/59173/5 "2006-05-03T18:56:59Z")

</div>

Place the code into a file called number.rb … you can put it in your  
models directory for example.

If it is in your models directory, then you don’t even need to require  
‘number’ - all you do is make a call to Number.to\_words and pass the  
amount as a parameter …

insight$ ls  
number.rb  
insight$ irb  
irb(main):001:0\> require ‘number’  
=\> true  
irb(main):002:0\> Number.to\_words 345678  
=\> “three hundred and fourty five thousand six hundred and seventy  
eight”  
irb(main):003:0\> exit  
insight$

---

<div class="post-metadata">

**Author:** ![idlefingers](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/898d66/32.png) [@idlefingers](https://www.ruby-forum.com/u/idlefingers)\
**Post date:** [May 1, 2006, 11:29am UTC](https://www.ruby-forum.com/t/format-numbers-as-words/59173/6 "2006-05-01T11:29:54Z")

</div>

Damien T. wrote:

> joey\_\_ wrote:
> 
> > Damien T. wrote:
> > 
> > > Does anyone know if there is a function available which can format any  
> > > entered number as words?
> > > 
> > > eg: ‘1234’ would be ‘One Thousand Two Hundred and Thirty Four’
> > > 
> > > In the past, I would have said this is a tall order, but from what I’ve  
> > > seen with playing with rails for a short time, I don’t know what to  
> > > expect. 🙂
> > > 
> > > Thanks for any help,  
> > > Damien  
> > > [Ruby Quiz - English Numerals (#25)](http://www.rubyquiz.com/quiz25.html)
> > 
> > j`ey  
> > [http://www.eachmapinject.com](http://www.eachmapinject.com)
> 
> Thanks, that looks perfect, but i’m really confused as to how I  
> implement it… it doesnt explain on that page how to actually use it??
> 
> Sorry, i’m a real noob with rails.

Add the code from:  
[http://www.ruby-talk.org/cgi-bin/scat.rb/ruby/ruby-talk/135735](http://www.ruby-talk.org/cgi-bin/scat.rb/ruby/ruby-talk/135735)

Into a english\_numbers.rb in the lib/ folder. Then do require  
‘lib/english\_numbers.rb’

Then do EnglishNumerals.to\_English(number)

j`ey  
[http://eachmapinject.com](http://eachmapinject.com)

---

<div class="post-metadata">

**Author:** ![idlefingers](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/898d66/32.png) [@idlefingers](https://www.ruby-forum.com/u/idlefingers)\
**Post date:** [May 3, 2006, 7:10pm UTC](https://www.ruby-forum.com/t/format-numbers-as-words/59173/7 "2006-05-03T19:10:34Z")

</div>

Does this do the trick? You can uncomment the commented lines at the  
end and run the script to see if it works. Improvements and better  
ways to do this are appreciated …

– G.

class Number

def self.to\_words(number)  
Number.new.to\_s(number)  
end

def self.commify(number)  
(s=number.to\_s;x=s.length;s).rjust(x+(3-(x%3))).gsub(/(\d)(?=\d{3}+(.\d\*)?$)/,  
‘\1,’)  
end

def initialize  
@unit = %w[zero one two three four five six seven eight nine]  
@teen = %w[ten eleven twelve thirteen fourteen fifteen sixteen  
seventeen eighteen nineteen]  
@tens = %w[zero ten twenty thirty fourty fifty sixty seventy eighty  
ninety]  
@qtys = %w[hundred thousand million billion trillion quadrillion  
quintillion]  
@zero = [“zero”]  
@hundred = “hundred”  
@sepr = “and”  
end

def to\_s(number)  
out = quantify(number).flatten  
for x in 0 … out.length - 1  
out[x] = nil if out[x] == @sepr && out[x+1] == @sepr  
out[x] = nil if out[x] == “,” && out[x+1] == “,”  
end  
out.compact!  
out = @zero if out.length == 1 && out[0] == @sepr  
out.pop while out.last == @sepr  
out.shift while out.first == @sepr  
out.join(’ ‘).gsub(/ ,/,’,’)  
end

private

def padded\_groups(v)  
out = []  
padded = (s=v.to\_s;x=s.length;s).rjust(x+(3-(x%3))).gsub(/ /,‘0’)  
padded.scan(/.{3}/)  
end

def wordify(v)  
out = []  
zero = ‘0’[0]  
h, t, u = v[0] - zero, v[1] - zero, v[2] - zero  
if h != 0  
out \<\< @unit[h]  
out \<\< @hundred  
end  
out \<\< @sepr if h != 0 && (t != 0 || u != 0)  
out \<\< @sepr if h == 0 && t == 0 && u != 0  
if t == 1  
out \<\< @teen[u]  
else  
out \<\< @tens[t] if t != 0  
out \<\< @unit[u] if u != 0  
end  
return out  
end

def quantify(v)  
v = padded\_groups(v).reverse  
cur = pos = v.length - 1  
out = []  
while pos \>= 0  
if v[pos] == ‘,’  
out \<\< ‘,’  
next  
end  
word = wordify(v[pos])  
if word[0] != nil  
out \<\< word  
out \<\< @qtys[cur] if cur != 0  
else  
out \<\< @sepr  
end  
cur -= 1  
pos -= 1  
end  
return out  
end

end

# for number in [

# 0,

# 1,

# 3,

# 11,

# 100,

# 1000,

# 1001,

# 1100,

# 1101,

# 1\_000\_001,

# 8\_000\_000\_000,

# 8\_000\_000\_001,

# 4\_567\_890\_923,

# 6\_804\_567\_890\_903,

# 5\_006\_804\_567\_890\_903

# ]

# print “#{Number.commify(number)}: #{Number.to\_words(number)}\n”

# end

---

<div class="post-metadata">

**Author:** ![idlefingers](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/898d66/32.png) [@idlefingers](https://www.ruby-forum.com/u/idlefingers)\
**Post date:** [September 22, 2006, 12:41pm UTC](https://www.ruby-forum.com/t/format-numbers-as-words/59173/8 "2006-09-22T12:41:22Z")

</div>

Just the job!!! thats a great class

Cheers

Tim

---

<div class="post-metadata">

**Author:** ![idlefingers](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/898d66/32.png) [@idlefingers](https://www.ruby-forum.com/u/idlefingers)\
**Post date:** [May 3, 2006, 7:19pm UTC](https://www.ruby-forum.com/t/format-numbers-as-words/59173/9 "2006-05-03T19:19:26Z")

</div>

thanks, Guido S., that did the trick.

when I tried it with the previous code from j`ey, it killed my server  
and I had to reboot, but with that new script, it’s perfect. thanks  
again.
