# Formatting to "Thousands"

**URL:** <https://rubytalk.org/t/formatting-to-thousands/25285>\
**Category:** ruby-talk\
**Created:** [28 February 2006 11:08 UTC](https://rubytalk.org/t/formatting-to-thousands/25285 "2006-02-28T11:08:36Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![Barrie\_Jarman](https://avatars.discourse-cdn.com/v4/letter/b/6de8d8/32.png) [@Barrie\_Jarman](https://rubytalk.org/u/Barrie_Jarman)\
**Post date:** [28 February 2006 11:08 UTC](https://rubytalk.org/t/formatting-to-thousands/25285/1 "2006-02-28T11:08:36Z")

</div>

A colleague of mine wishes to format a float to include commas at thousand  
values, for example  
12345.21 becomes  
12,345.21

Is there any inbuilt functionality to format this, or are we better of  
writing something ourselves...

> **···**
>
> --  
> Posted via [NewsDemon.com](http://NewsDemon.com) - Premium Uncensored Newsgroup Service  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;-------\>\>\>\>\>\>[http://www.NewsDemon.com](http://www.NewsDemon.com)\<\<\<\<\<\<------  
> Unlimited Access, Anonymous Accounts, Uncensored Broadband Access

---

<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:** [28 February 2006 13:53 UTC](https://rubytalk.org/t/formatting-to-thousands/25285/2 "2006-02-28T13:53:34Z")

</div>

It's not built-in, but it's also pretty easy to roll one:

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

Hope that helps.

James Edward Gray II

> **···**
>
> On Feb 28, 2006, at 5:08 AM, Barrie Jarman wrote:
> 
> > A colleague of mine wishes to format a float to include commas at thousand  
> > values, for example  
> > 12345.21 becomes  
> > 12,345.21
> > 
> > Is there any inbuilt functionality to format this, or are we better of  
> > writing something ourselves...

---

<div class="post-metadata">

**Author:** ![Shane\_Emmons](https://avatars.discourse-cdn.com/v4/letter/s/65b543/32.png) [@Shane\_Emmons](https://rubytalk.org/u/Shane_Emmons)\
**Post date:** [28 February 2006 16:13 UTC](https://rubytalk.org/t/formatting-to-thousands/25285/3 "2006-02-28T16:13:38Z")

</div>

I have to say, that is some great Regex Fu. I was looking to do  
something similar but your solution is much nicer!

---

<div class="post-metadata">

**Author:** ![Barrie\_Jarman](https://avatars.discourse-cdn.com/v4/letter/b/6de8d8/32.png) [@Barrie\_Jarman](https://rubytalk.org/u/Barrie_Jarman)\
**Post date:** [28 February 2006 16:48 UTC](https://rubytalk.org/t/formatting-to-thousands/25285/4 "2006-02-28T16:48:38Z")

</div>

Implemented this into the float class.  
Adds check to make sure 0 is handled properly and adds for two decimal  
places (sorry, probably wasn't specific enough before).

def to\_finance  
&nbsp;&nbsp;if self != 0  
&nbsp;&nbsp;&nbsp;number = sprintf("%.2f", self)  
&nbsp;&nbsp;&nbsp;&nbsp;number.to\_s.reverse.gsub!(/(\d\d\d)(?=\d)(?!\d\*\.)/, '\1,').reverse  
&nbsp;&nbsp;&nbsp;else  
&nbsp;&nbsp;&nbsp;&nbsp;sprintf("%.2f", self)  
&nbsp;&nbsp;&nbsp;end  
end

"James Edward Gray II" \<james@grayproductions.net\> wrote in message  
news:47AC1DDA-5D15-461F-AEB2-40B5E23903B1@grayproductions.net...

> **···**
>
> > On Feb 28, 2006, at 5:08 AM, Barrie Jarman wrote:
> > 
> > > A colleague of mine wishes to format a float to include commas at  
> > > thousand  
> > > values, for example  
> > > 12345.21 becomes  
> > > 12,345.21
> > > 
> > > Is there any inbuilt functionality to format this, or are we better of  
> > > writing something ourselves...
> > 
> > It's not built-in, but it's also pretty easy to roll one:
> > 
> > def commify( number )  
> > &nbsp;&nbsp;number.to\_s.reverse.gsub!(/(\d\d\d)(?=\d)(?!\d\*\.)/, '\1,').reverse  
> > end
> > 
> > Hope that helps.
> > 
> > James Edward Gray II
> 
> --  
> Posted via NewsDemon.com - Premium Uncensored Newsgroup Service  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;-------\>\>\>\>\>\>[http://www.NewsDemon.com](http://www.NewsDemon.com)\<\<\<\<\<\<------  
> Unlimited Access, Anonymous Accounts, Uncensored Broadband Access

---

<div class="post-metadata">

**Author:** ![Jeff\_Schwab](https://avatars.discourse-cdn.com/v4/letter/j/b5ac83/32.png) [@Jeff\_Schwab](https://rubytalk.org/u/Jeff_Schwab)\
**Post date:** [28 February 2006 16:53 UTC](https://rubytalk.org/t/formatting-to-thousands/25285/5 "2006-02-28T16:53:36Z")

</div>

James Edward Gray II wrote:

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

That's nice, except that gsub! returns nil if no substitutions are performed, e.g. if the number has fewer than 4 digits. This can result in an exception like:

&nbsp;&nbsp;&nbsp;main.rb:3:in `commify':  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;undefined method `reverse' for nil:NilClass(NoMethodError)

Of course, you could just break the chain across multiple lines:

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

---

<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:** [28 February 2006 16:59 UTC](https://rubytalk.org/t/formatting-to-thousands/25285/6 "2006-02-28T16:59:37Z")

</div>

Good catch. I originally had it in multiple lines and I forgot to remove the ! when I shortened it for this post.

Thanks.

James Edward Gray II

> **···**
>
> On Feb 28, 2006, at 10:53 AM, Jeffrey Schwab wrote:
> 
> > James Edward Gray II wrote:
> > 
> > > def commify( number )  
> > > &nbsp;&nbsp;number.to\_s.reverse.gsub!(/(\d\d\d)(?=\d)(?!\d\*\.)/, '\1,').reverse  
> > > end
> > 
> > That's nice, except that gsub! returns nil if no substitutions are performed, e.g. if the number has fewer than 4 digits. This can result in an exception like:
> > 
> > &nbsp;&nbsp;main.rb:3:in `commify':  
> > &nbsp;&nbsp;&nbsp;&nbsp;undefined method `reverse' for nil:NilClass(NoMethodError)
> > 
> > Of course, you could just break the chain across multiple lines:
> > 
> > &nbsp;&nbsp;def commify(number)  
> > &nbsp;&nbsp;&nbsp;&nbsp;s = number.to\_s  
> > &nbsp;&nbsp;&nbsp;&nbsp;s.reverse!  
> > &nbsp;&nbsp;&nbsp;&nbsp;s.gsub!(/(\d\d\d)(?=\d)(?!\d\*\.)/, '\1,')  
> > &nbsp;&nbsp;&nbsp;&nbsp;s.reverse!  
> > &nbsp;&nbsp;end

---

<div class="post-metadata">

**Author:** ![Kevin\_Olbrich](https://avatars.discourse-cdn.com/v4/letter/k/59ef9b/32.png) [@Kevin\_Olbrich](https://rubytalk.org/u/Kevin_Olbrich)\
**Post date:** [28 February 2006 17:02 UTC](https://rubytalk.org/t/formatting-to-thousands/25285/7 "2006-02-28T17:02:48Z")

</div>

Or you could just use 'gsub' instead of 'gsub!' and avoid the nil

\_Kevin

> **···**
>
> -----Original Message-----  
> From: Jeffrey Schwab [mailto:jeff@schwabcenter.com]  
> Sent: Tuesday, February 28, 2006 11:54 AM  
> To: ruby-talk ML  
> Subject: Re: Formatting to "Thousands"
> 
> James Edward Gray II wrote:
> 
> > def commify( number )  
> > &nbsp;&nbsp;number.to\_s.reverse.gsub!(/(\d\d\d)(?=\d)(?!\d\*\.)/, '\1,').reverse  
> > end
> 
> That's nice, except that gsub! returns nil if no substitutions are  
> performed, e.g. if the number has fewer than 4 digits. This can result in  
> an exception like:
> 
> &nbsp;&nbsp;&nbsp;main.rb:3:in `commify':  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;undefined method `reverse' for nil:NilClass(NoMethodError)
> 
> Of course, you could just break the chain across multiple lines:
> 
> &nbsp;&nbsp;&nbsp;def commify(number)  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;s = number.to\_s  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;s.reverse!  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;s.gsub!(/(\d\d\d)(?=\d)(?!\d\*\.)/, '\1,')  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;s.reverse!  
> &nbsp;&nbsp;&nbsp;end

---

<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:** [1 March 2006 03:56 UTC](https://rubytalk.org/t/formatting-to-thousands/25285/8 "2006-03-01T03:56:55Z")

</div>

I don't know why but using regexps to do this kind of scares me. Maybe its cause I don't have as much regexp fu but it just seems like  
ruby  
perl  
ruby

Not that I have anything against perl or regexps, but that particular regexp seems to scream "I'm doing this with a regexp not because it's easier but because it's faster and shorter." Not that that's wrong. Hmm I seem to be trying really hard not to offend anyone :-p. I guess a rule of thumb for me is more than 1 lookahead assertion and my eyes glaze over 😉

> **···**
>
> On Feb 28, 2006, at 11:59 AM, James Edward Gray II wrote:
> 
> > On Feb 28, 2006, at 10:53 AM, Jeffrey Schwab wrote:
> > 
> > > James Edward Gray II wrote:
> > > 
> > > > def commify( number )  
> > > > &nbsp;&nbsp;number.to\_s.reverse.gsub!(/(\d\d\d)(?=\d)(?!\d\*\.)/, '\1,').reverse  
> > > > end
> > > 
> > > That's nice, except that gsub! returns nil if no substitutions are performed, e.g. if the number has fewer than 4 digits. This can result in an exception like:
> > > 
> > > &nbsp;&nbsp;main.rb:3:in `commify':  
> > > &nbsp;&nbsp;&nbsp;&nbsp;undefined method `reverse' for nil:NilClass(NoMethodError)
> > > 
> > > Of course, you could just break the chain across multiple lines:
> > > 
> > > &nbsp;&nbsp;def commify(number)  
> > > &nbsp;&nbsp;&nbsp;&nbsp;s = number.to\_s  
> > > &nbsp;&nbsp;&nbsp;&nbsp;s.reverse!  
> > > &nbsp;&nbsp;&nbsp;&nbsp;s.gsub!(/(\d\d\d)(?=\d)(?!\d\*\.)/, '\1,')  
> > > &nbsp;&nbsp;&nbsp;&nbsp;s.reverse!  
> > > &nbsp;&nbsp;end
> > 
> > Good catch. I originally had it in multiple lines and I forgot to remove the ! when I shortened it for this post.
> > 
> > Thanks.
> > 
> > James Edward Gray II

---

<div class="post-metadata">

**Author:** ![Jeff\_Schwab](https://avatars.discourse-cdn.com/v4/letter/j/b5ac83/32.png) [@Jeff\_Schwab](https://rubytalk.org/u/Jeff_Schwab)\
**Post date:** [1 March 2006 16:23 UTC](https://rubytalk.org/t/formatting-to-thousands/25285/9 "2006-03-01T16:23:37Z")

</div>

Logan Capaldo wrote:

> > > James Edward Gray II wrote:
> > > 
> > > > def commify( number )  
> > > > &nbsp;&nbsp;number.to\_s.reverse.gsub!(/(\d\d\d)(?=\d)(?!\d\*\.)/, '\1,').reverse  
> > > > end
> > > 
> > > That's nice, except that gsub! returns nil if no substitutions are performed, e.g. if the number has fewer than 4 digits. This can result in an exception like:
> > > 
> > > &nbsp;&nbsp;main.rb:3:in `commify':  
> > > &nbsp;&nbsp;&nbsp;&nbsp;undefined method `reverse' for nil:NilClass(NoMethodError)
> > > 
> > > Of course, you could just break the chain across multiple lines:
> > > 
> > > &nbsp;&nbsp;def commify(number)  
> > > &nbsp;&nbsp;&nbsp;&nbsp;s = number.to\_s  
> > > &nbsp;&nbsp;&nbsp;&nbsp;s.reverse!  
> > > &nbsp;&nbsp;&nbsp;&nbsp;s.gsub!(/(\d\d\d)(?=\d)(?!\d\*\.)/, '\1,')  
> > > &nbsp;&nbsp;&nbsp;&nbsp;s.reverse!  
> > > &nbsp;&nbsp;end
> > 
> > Good catch. I originally had it in multiple lines and I forgot to remove the ! when I shortened it for this post.
> > 
> > Thanks.
> > 
> > James Edward Gray II
> 
> I don't know why but using regexps to do this kind of scares me. Maybe its cause I don't have as much regexp fu but it just seems like  
> ruby  
> perl  
> ruby
> 
> Not that I have anything against perl or regexps, but that particular regexp seems to scream "I'm doing this with a regexp not because it's easier but because it's faster and shorter." Not that that's wrong. Hmm I seem to be trying really hard not to offend anyone :-p. I guess a rule of thumb for me is more than 1 lookahead assertion and my eyes glaze over 😉

Try the Friedl book. It will open your eyes. 🙂

> **···**
>
> > On Feb 28, 2006, at 11:59 AM, James Edward Gray II wrote:
> > 
> > > On Feb 28, 2006, at 10:53 AM, Jeffrey Schwab wrote:
