# Formatted output for numbers with comma's?

**URL:** <https://www.ruby-forum.com/t/formatted-output-for-numbers-with-commas/54789>\
**Category:** Ruby\
**Created:** [March 14, 2006, 9:14am UTC](https://www.ruby-forum.com/t/formatted-output-for-numbers-with-commas/54789 "2006-03-14T09:14:08Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Wink\_Saville](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/439d5e/32.png) [@Wink\_Saville](https://www.ruby-forum.com/u/Wink_Saville)\
**Post date:** [March 14, 2006, 9:14am UTC](https://www.ruby-forum.com/t/formatted-output-for-numbers-with-commas/54789/1 "2006-03-14T09:14:08Z")

</div>

I was looking for a routine to convert a Number to a string with  
comma’s, is there a routine in the library for that?

Wink

---

<div class="post-metadata">

**Author:** ![Wink\_Saville](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/439d5e/32.png) [@Wink\_Saville](https://www.ruby-forum.com/u/Wink_Saville)\
**Post date:** [March 14, 2006, 9:44am UTC](https://www.ruby-forum.com/t/formatted-output-for-numbers-with-commas/54789/2 "2006-03-14T09:44:55Z")

</div>

Wink Saville wrote:

> I was looking for a routine to convert a Number to a string with  
> comma’s, is there a routine in the library for that?

[http://rubyforge.org/snippet/detail.php?type=snippet&id=8](http://rubyforge.org/snippet/detail.php?type=snippet&id=8)

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

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

[http://globalize-rails.org/wiki/](http://globalize-rails.org/wiki/)

---

<div class="post-metadata">

**Author:** ![Wink\_Saville](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/439d5e/32.png) [@Wink\_Saville](https://www.ruby-forum.com/u/Wink_Saville)\
**Post date:** [March 14, 2006, 2:49pm UTC](https://www.ruby-forum.com/t/formatted-output-for-numbers-with-commas/54789/3 "2006-03-14T14:49:00Z")

</div>

On Mar 14, 2006, at 2:11 AM, Wink Saville wrote:

> I was looking for a routine to convert a Number to a string with  
> comma’s, is there a routine in the library for that?

It’s not very hard to roll up a solution:

def commify( number )  
number.to\_s.reverse.gsub(/(\d\d\d)(?=\d)(?!\d\*.)/, ‘\1,’).reverse  
end

Hope that helps.

James Edward G. II

---

<div class="post-metadata">

**Author:** ![Wink\_Saville](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/439d5e/32.png) [@Wink\_Saville](https://www.ruby-forum.com/u/Wink_Saville)\
**Post date:** [March 15, 2006, 2:28am UTC](https://www.ruby-forum.com/t/formatted-output-for-numbers-with-commas/54789/4 "2006-03-15T02:28:05Z")

</div>

This would be a nice (ruby builtin) method 🙂

def commify( number, positions)  
positions = 3 if positions == nil  
puts number.to\_s.reverse.gsub(/(\d\d\d)(?=\d)(?!\d\*.)/,  
‘\1,’).reverse  
^ not very sure how it would be done but something similar to  
("\d"\*positions)  
end

puts commify(12345) # 12,345  
puts commify(12345, 2) # 1,23,45

---

<div class="post-metadata">

**Author:** ![Wink\_Saville](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/439d5e/32.png) [@Wink\_Saville](https://www.ruby-forum.com/u/Wink_Saville)\
**Post date:** [March 20, 2006, 12:31am UTC](https://www.ruby-forum.com/t/formatted-output-for-numbers-with-commas/54789/5 "2006-03-20T00:31:17Z")

</div>

- @ 15/03/06 01:27:40 AM removed\_email\_address@domain.invalid wrote:

> This would be a nice (ruby builtin) method 🙂

+1 vote as I’ve needed it quite a bit.

How would I/we go about getting something like this in stdlib?

have fun,

SteveC removed\_email\_address@domain.invalid [http://www.asklater.com/steve/](http://www.asklater.com/steve/)

---

<div class="post-metadata">

**Author:** ![Wink\_Saville](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/439d5e/32.png) [@Wink\_Saville](https://www.ruby-forum.com/u/Wink_Saville)\
**Post date:** [March 20, 2006, 1:11am UTC](https://www.ruby-forum.com/t/formatted-output-for-numbers-with-commas/54789/6 "2006-03-20T01:11:29Z")

</div>

On 3/14/06, James Edward G. II [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> On Mar 14, 2006, at 2:11 AM, Wink Saville wrote:
> 
> > I was looking for a routine to convert a Number to a string with  
> > comma’s, is there a routine in the library for that?
> 
> It’s not very hard to roll up a solution:
> 
> def commify( number )  
> number.to\_s.reverse.gsub(/(\d\d\d)(?=\d)(?!\d\*.)/, ‘\1,’).reverse  
> end

…it’s significantly harder to do it completely _right_, though.

See Gavin S.'s Extensions library in Numeric. I have a  
relatively complete method written there, done in test-first style.

-austin
