# Formatting Numbers

**URL:** <https://rubytalk.org/t/formatting-numbers/24756>\
**Category:** ruby-talk\
**Created:** [9 February 2006 01:43 UTC](https://rubytalk.org/t/formatting-numbers/24756 "2006-02-09T01:43:35Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![HH1](https://avatars.discourse-cdn.com/v4/letter/h/4491bb/32.png) [@HH1](https://rubytalk.org/u/HH1)\
**Post date:** [9 February 2006 01:43 UTC](https://rubytalk.org/t/formatting-numbers/24756/1 "2006-02-09T01:43:35Z")

</div>

This has got to be simple but I checked the PickAxe and Google to no avail.  
My guess is that I'm looking for it in terms that don't make sense.

Anyway...

I have a fixnum:

a = 1000

I would like to output it with a comma, such as "1,000". Is there a  
formatting class or something I can use something along these lines  
(thinking from my Java background):

Formatter f = Formatter.new("N0")  
puts f(a)

Any help appreciated.

Thanks,  
Hunter

---

<div class="post-metadata">

**Author:** ![Logan\_Capaldo](https://avatars.discourse-cdn.com/v4/letter/l/7ea924/32.png) [@Logan\_Capaldo](https://rubytalk.org/u/Logan_Capaldo)\
**Post date:** [9 February 2006 02:11 UTC](https://rubytalk.org/t/formatting-numbers/24756/2 "2006-02-09T02:11:21Z")

</div>

I dunno know about some kind of builtin format object (I imagine maybe the right incantation of sprintf could do it) but heres a method (kinda long for what it is, theres probably some regex to do it in one fell swoop):

% cat commify.rb  
require 'enumerator'  
def commify(num)  
&nbsp;&nbsp;&nbsp;str = num.to\_s  
&nbsp;&nbsp;&nbsp;a =   
&nbsp;&nbsp;&nbsp;str.split(//).reverse.each\_slice(3) { |slice| a \<\< slice }  
&nbsp;&nbsp;&nbsp;new\_a =   
&nbsp;&nbsp;&nbsp;a.each do |item|  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;new\_a \<\< item  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;new\_a \<\< [","]  
&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;&nbsp;new\_a.delete\_at(new\_a.length - 1)  
&nbsp;&nbsp;&nbsp;new\_a.flatten.reverse.join  
end

p commify(1000)  
p commify(100)  
p commify(1\_000\_000)  
p commify(1024)

% ruby commify.rb  
"1,000"  
"100"  
"1,000,000"  
"1,024"

> **···**
>
> On Feb 8, 2006, at 8:43 PM, HH wrote:
> 
> > This has got to be simple but I checked the PickAxe and Google to no avail.  
> > My guess is that I'm looking for it in terms that don't make sense.
> > 
> > Anyway...
> > 
> > I have a fixnum:
> > 
> > a = 1000
> > 
> > I would like to output it with a comma, such as "1,000". Is there a  
> > formatting class or something I can use something along these lines  
> > (thinking from my Java background):
> > 
> > Formatter f = Formatter.new("N0")  
> > puts f(a)
> > 
> > Any help appreciated.
> > 
> > Thanks,  
> > Hunter

---

<div class="post-metadata">

**Author:** ![konsu](https://avatars.discourse-cdn.com/v4/letter/k/0ea827/32.png) [@konsu](https://rubytalk.org/u/konsu)\
**Post date:** [9 February 2006 03:28 UTC](https://rubytalk.org/t/formatting-numbers/24756/3 "2006-02-09T03:28:22Z")

</div>

hello,

there is even bigger problem: the formatting that you are looking for  
depends on locale, and as far as i could tell ruby does not provide any  
support for that. so the solutions that people suggested would work only for  
american formatting, and as soon as you choose to change your formatting and  
use comma for decimal point you are at the square one...

konstantin

"HH" \<lists@lastonepicked.com\> wrote in message  
news:C00FDE38.BE9B2%lists@lastonepicked.com...

> **···**
>
> > This has got to be simple but I checked the PickAxe and Google to no  
> > avail.  
> > My guess is that I'm looking for it in terms that don't make sense.
> > 
> > Anyway...
> > 
> > I have a fixnum:
> > 
> > a = 1000
> > 
> > I would like to output it with a comma, such as "1,000". Is there a  
> > formatting class or something I can use something along these lines  
> > (thinking from my Java background):
> > 
> > Formatter f = Formatter.new("N0")  
> > puts f(a)
> > 
> > Any help appreciated.
> > 
> > Thanks,  
> > Hunter

---

<div class="post-metadata">

**Author:** ![James\_Edward\_Gray\_II](https://avatars.discourse-cdn.com/v4/letter/j/ea5d25/32.png) [@James\_Edward\_Gray\_II](https://rubytalk.org/u/James_Edward_Gray_II)\
**Post date:** [9 February 2006 03:47 UTC](https://rubytalk.org/t/formatting-numbers/24756/4 "2006-02-09T03:47:34Z")

</div>

def commify( str )  
&nbsp;&nbsp;&nbsp;str.reverse.gsub(/(\d\d\d)(?=\d)(?!\d\*\.)/,'\1,').reverse  
end

Handles floats too.

James Edward Gray II

> **···**
>
> On Feb 8, 2006, at 7:43 PM, HH wrote:
> 
> > This has got to be simple but I checked the PickAxe and Google to no avail.  
> > My guess is that I'm looking for it in terms that don't make sense.
> > 
> > Anyway...
> > 
> > I have a fixnum:
> > 
> > a = 1000
> > 
> > I would like to output it with a comma, such as "1,000". Is there a  
> > formatting class or something I can use something along these lines  
> > (thinking from my Java background):
> > 
> > Formatter f = Formatter.new("N0")  
> > puts f(a)
> > 
> > Any help appreciated.

---

<div class="post-metadata">

**Author:** ![Gene\_Tani](https://avatars.discourse-cdn.com/v4/letter/g/4491bb/32.png) [@Gene\_Tani](https://rubytalk.org/u/Gene_Tani)\
**Post date:** [9 February 2006 03:53 UTC](https://rubytalk.org/t/formatting-numbers/24756/5 "2006-02-09T03:53:22Z")

</div>

HH wrote:

> I have a fixnum:
> 
> a = 1000
> 
> I would like to output it with a comma, such as "1,000". Is there a  
> formatting class or something I can use something along these lines  
> (thinking from my Java background):

[http://extensions.rubyforge.org/rdoc/classes/Numeric.html#M000011](http://extensions.rubyforge.org/rdoc/classes/Numeric.html#M000011)

---

<div class="post-metadata">

**Author:** ![Kent\_Sibilev1](https://avatars.discourse-cdn.com/v4/letter/k/9d8465/32.png) [@Kent\_Sibilev1](https://rubytalk.org/u/Kent_Sibilev1)\
**Post date:** [9 February 2006 02:25 UTC](https://rubytalk.org/t/formatting-numbers/24756/6 "2006-02-09T02:25:33Z")

</div>

Or

irb(main):001:0\> class Integer  
irb(main):002:1\> def commify  
irb(main):003:2\> self.to\_s.gsub(/(\d)(?=(\d{3})+$)/,'\1,')  
irb(main):004:2\> end  
irb(main):005:1\> end  
=\> nil  
irb(main):006:0\> 1000.commify  
=\> "1,000"  
irb(main):007:0\> 100.commify  
=\> "100"  
irb(main):008:0\> 1\_000\_000.commify  
=\> "1,000,000"  
irb(main):009:0\> 1024.commify  
=\> "1,024"

Kent

> **···**
>
> On 2/8/06, Logan Capaldo \<logancapaldo@gmail.com\> wrote:
> 
> > On Feb 8, 2006, at 8:43 PM, HH wrote:
> > 
> > \> This has got to be simple but I checked the PickAxe and Google to  
> > \> no avail.  
> > \> My guess is that I'm looking for it in terms that don't make sense.  
> > \>  
> > \> Anyway...  
> > \>  
> > \> I have a fixnum:  
> > \>  
> > \> a = 1000  
> > \>  
> > \> I would like to output it with a comma, such as "1,000". Is there a  
> > \> formatting class or something I can use something along these lines  
> > \> (thinking from my Java background):  
> > \>  
> > \> Formatter f = Formatter.new("N0")  
> > \> puts f(a)  
> > \>  
> > \> Any help appreciated.  
> > \>  
> > \> Thanks,  
> > \> Hunter  
> > \>  
> > \>  
> > \>
> > 
> > I dunno know about some kind of builtin format object (I imagine  
> > maybe the right incantation of sprintf could do it) but heres a  
> > method (kinda long for what it is, theres probably some regex to do  
> > it in one fell swoop):
> > 
> > % cat commify.rb  
> > require 'enumerator'  
> > def commify(num)  
> > &nbsp;&nbsp;&nbsp;str = num.to\_s  
> > &nbsp;&nbsp;&nbsp;a =   
> > &nbsp;&nbsp;&nbsp;str.split(//).reverse.each\_slice(3) { |slice| a \<\< slice }  
> > &nbsp;&nbsp;&nbsp;new\_a =   
> > &nbsp;&nbsp;&nbsp;a.each do |item|  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;new\_a \<\< item  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;new\_a \<\< [","]  
> > &nbsp;&nbsp;&nbsp;end  
> > &nbsp;&nbsp;&nbsp;new\_a.delete\_at(new\_a.length - 1)  
> > &nbsp;&nbsp;&nbsp;new\_a.flatten.reverse.join  
> > end
> > 
> > p commify(1000)  
> > p commify(100)  
> > p commify(1\_000\_000)  
> > p commify(1024)
> > 
> > % ruby commify.rb  
> > "1,000"  
> > "100"  
> > "1,000,000"  
> > "1,024"

---

<div class="post-metadata">

**Author:** ![David\_Vallner](https://avatars.discourse-cdn.com/v4/letter/d/eb9ed0/32.png) [@David\_Vallner](https://rubytalk.org/u/David_Vallner)\
**Post date:** [9 February 2006 02:27 UTC](https://rubytalk.org/t/formatting-numbers/24756/7 "2006-02-09T02:27:38Z")

</div>

Dňa Štvrtok 09 Február 2006 03:11 Logan Capaldo napísal:

> \> This has got to be simple but I checked the PickAxe and Google to  
> \> no avail.  
> \> My guess is that I'm looking for it in terms that don't make sense.  
> \>  
> \> Anyway...  
> \>  
> \> I have a fixnum:  
> \>  
> \> a = 1000  
> \>  
> \> I would like to output it with a comma, such as "1,000". Is there a  
> \> formatting class or something I can use something along these lines  
> \> (thinking from my Java background):  
> \>  
> \> Formatter f = Formatter.new("N0")  
> \> puts f(a)  
> \>  
> \> Any help appreciated.  
> \>  
> \> Thanks,  
> \> Hunter
> 
> I dunno know about some kind of builtin format object (I imagine  
> maybe the right incantation of sprintf could do it) but heres a  
> method (kinda long for what it is, theres probably some regex to do  
> it in one fell swoop):
> 
> % cat commify.rb  
> require 'enumerator'  
> def commify(num)  
> &nbsp;&nbsp;&nbsp;str = num.to\_s  
> &nbsp;&nbsp;&nbsp;a =   
> &nbsp;&nbsp;&nbsp;str.split(//).reverse.each\_slice(3) { |slice| a \<\< slice }  
> &nbsp;&nbsp;&nbsp;new\_a =   
> &nbsp;&nbsp;&nbsp;a.each do |item|  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;new\_a \<\< item  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;new\_a \<\< [","]  
> &nbsp;&nbsp;&nbsp;end  
> &nbsp;&nbsp;&nbsp;new\_a.delete\_at(new\_a.length - 1)  
> &nbsp;&nbsp;&nbsp;new\_a.flatten.reverse.join  
> end
> 
> p commify(1000)  
> p commify(100)  
> p commify(1\_000\_000)  
> p commify(1024)
> 
> % ruby commify.rb  
> "1,000"  
> "100"  
> "1,000,000"  
> "1,024"

Hmm, I was in the middle of writing precisely this sort of hack as this  
arrived. That said, much better job here. The #each\_slice comes from the  
enumerator library? I should really learn to use that one...

That said, I'm almost sure you can golf that thing down a bit. How about:

&nbsp;&nbsp;require 'enumerator'  
&nbsp;&nbsp;def commify(num)  
&nbsp;&nbsp;&nbsp;&nbsp;str = num.to\_s  
&nbsp;&nbsp;&nbsp;&nbsp;a =   
&nbsp;&nbsp;&nbsp;&nbsp;str.split(//).reverse.each\_slice(3) { |slice| a \<\< slice }  
&nbsp;&nbsp;&nbsp;&nbsp;a.reverse.collect{|i| i.reverse.join}.join(",")  
&nbsp;&nbsp;end

&nbsp;&nbsp;p commify(1000)  
&nbsp;&nbsp;p commify(100)  
&nbsp;&nbsp;p commify(1\_000\_000)  
&nbsp;&nbsp;p commify(1024)

Now if only we directly had a method to slice an Array into an Array of  
slices, the whole thing could turn into a one-liner. Which I hate, but in a  
good way.

David Vallner

> **···**
>
> > On Feb 8, 2006, at 8:43 PM, HH wrote:

---

<div class="post-metadata">

**Author:** ![HH1](https://avatars.discourse-cdn.com/v4/letter/h/4491bb/32.png) [@HH1](https://rubytalk.org/u/HH1)\
**Post date:** [9 February 2006 04:58 UTC](https://rubytalk.org/t/formatting-numbers/24756/8 "2006-02-09T04:58:42Z")

</div>

Thanks to everyone for all the great suggestions.

I really appreciate it.

> **···**
>
> > From: Gene Tani \<gene.tani@gmail.com\>  
> > Organization: [http://groups.google.com](http://groups.google.com)  
> > Reply-To: \<ruby-talk@ruby-lang.org\>  
> > Newsgroups: comp.lang.ruby  
> > Date: Thu, 9 Feb 2006 12:53:22 +0900  
> > To: ruby-talk ML \<ruby-talk@ruby-lang.org\>  
> > Subject: Re: Formatting Numbers
> > 
> > HH wrote:
> > 
> > > I have a fixnum:
> > > 
> > > a = 1000
> > > 
> > > I would like to output it with a comma, such as "1,000". Is there a  
> > > formatting class or something I can use something along these lines  
> > > (thinking from my Java background):
> > 
> > [http://extensions.rubyforge.org/rdoc/classes/Numeric.html#M000011](http://extensions.rubyforge.org/rdoc/classes/Numeric.html#M000011)

---

<div class="post-metadata">

**Author:** ![David\_Vallner](https://avatars.discourse-cdn.com/v4/letter/d/eb9ed0/32.png) [@David\_Vallner](https://rubytalk.org/u/David_Vallner)\
**Post date:** [9 February 2006 13:55 UTC](https://rubytalk.org/t/formatting-numbers/24756/9 "2006-02-09T13:55:12Z")

</div>

Hmm.

> **[Ruby Locale](https://sourceforge.net/projects/ruby-locale/)**
>
> Download Ruby Locale for free. Provide an extension to the Ruby intepreter to support locales

Anyone have any experience with that?

David Vallner

Dňa Štvrtok 09 Február 2006 04:28 konsu napísal:

> **···**
>
> > hello,
> > 
> > there is even bigger problem: the formatting that you are looking for  
> > depends on locale, and as far as i could tell ruby does not provide any  
> > support for that. so the solutions that people suggested would work only  
> > for american formatting, and as soon as you choose to change your  
> > formatting and use comma for decimal point you are at the square one...
> > 
> > konstantin
> > 
> > "HH" \<lists@lastonepicked.com\> wrote in message  
> > news:C00FDE38.BE9B2%lists@lastonepicked.com...

---

<div class="post-metadata">

**Author:** ![Logan\_Capaldo](https://avatars.discourse-cdn.com/v4/letter/l/7ea924/32.png) [@Logan\_Capaldo](https://rubytalk.org/u/Logan_Capaldo)\
**Post date:** [9 February 2006 02:43 UTC](https://rubytalk.org/t/formatting-numbers/24756/10 "2006-02-09T02:43:19Z")

</div>

str.split(//).reverse.to\_enum(:each\_slice,3).to\_a

> **···**
>
> On Feb 8, 2006, at 9:27 PM, David Vallner wrote:
> 
> > Now if only we directly had a method to slice an Array into an Array of  
> > slices, the whole thing could turn into a one-liner. Which I hate, but in a  
> > good way.
> > 
> > David Vallner

---

<div class="post-metadata">

**Author:** ![Michael\_Nissim](https://avatars.discourse-cdn.com/v4/letter/m/47e85d/32.png) [@Michael\_Nissim](https://rubytalk.org/u/Michael_Nissim)\
**Post date:** [4 July 2010 09:07 UTC](https://rubytalk.org/t/formatting-numbers/24756/11 "2010-07-04T09:07:11Z")

</div>

Logan Capaldo wrote:

> **···**
>
> > On Feb 8, 2006, at 9:27 PM, David Vallner wrote:
> > 
> > > Now if only we directly had a method to slice an Array into an  
> > > Array of  
> > > slices, the whole thing could turn into a one-liner. Which I hate,  
> > > but in a  
> > > good way.
> > > 
> > > David Vallner
> > 
> > str.split(//).reverse.to\_enum(:each\_slice,3).to\_a
> 
> The one above from Logan didn't work for me.
> 
> But this one from James Edward Gray II did:  
> str.reverse.gsub(/(\d\d\d)(?=\d)(?!\d\*\.)/,'\1,').reverse
> 
> Thanks to both.  
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).
