# Putting commas in floats to seperate hundreds from thousands

**URL:** <https://www.ruby-forum.com/t/putting-commas-in-floats-to-seperate-hundreds-from-thousands/83268>\
**Category:** Ruby\
**Created:** [January 24, 2007, 7:45pm UTC](https://www.ruby-forum.com/t/putting-commas-in-floats-to-seperate-hundreds-from-thousands/83268 "2007-01-24T19:45:17Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![johnk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/59ef9b/32.png) [@johnk](https://www.ruby-forum.com/u/johnk)\
**Post date:** [January 24, 2007, 7:45pm UTC](https://www.ruby-forum.com/t/putting-commas-in-floats-to-seperate-hundreds-from-thousands/83268/1 "2007-01-24T19:45:17Z")

</div>

I have the following code:

class Float  
def to\_dollars\_with\_cents  
format(“$%.2f”, self)  
end  
end

This way in rails I can go:

@summary.job\_cost.to\_dollars\_with\_cents

The output comes out as: $1900000.00

I would prefer to format it as: $1,900,000.00

How would you suggest I do that?

Your Friend,

John

–  
John K.  
removed\_email\_address@domain.invalid

[http://www.kopanas.com](http://www.kopanas.com)

> **[Canadian University Software Engineering Conference - CUSEC](http://cusec.net/)**
>
> Canadian University Software Engineering Conference is a yearly conference created by students for students.

[http://www.soen.info](http://www.soen.info)

---

<div class="post-metadata">

**Author:** ![johnk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/59ef9b/32.png) [@johnk](https://www.ruby-forum.com/u/johnk)\
**Post date:** [January 24, 2007, 8:06pm UTC](https://www.ruby-forum.com/t/putting-commas-in-floats-to-seperate-hundreds-from-thousands/83268/2 "2007-01-24T20:06:22Z")

</div>

On Jan 24, 11:44 am, “John K.” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> This way in rails I can go:
> 
> @summary.job\_cost.to\_dollars\_with\_cents

Easier:  
[http://api.rubyonrails.com/classes/ActionView/Helpers/NumberHelper.html#M000517](http://api.rubyonrails.com/classes/ActionView/Helpers/NumberHelper.html#M000517)

---

<div class="post-metadata">

**Author:** ![johnk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/59ef9b/32.png) [@johnk](https://www.ruby-forum.com/u/johnk)\
**Post date:** [January 24, 2007, 9:05pm UTC](https://www.ruby-forum.com/t/putting-commas-in-floats-to-seperate-hundreds-from-thousands/83268/3 "2007-01-24T21:05:33Z")

</div>

On Jan 24, 11:01 am, “Phrogz” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> On Jan 24, 11:44 am, “John K.” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> > This way in rails I can go:
> 
> > @summary.job\_cost.to\_dollars\_with\_centsEasier:[Peak Obsession](http://api.rubyonrails.com/classes/ActionView/Helpers/NumberHelper.ht)…

The latest edition of Mastering Regular Expressions suggests the  
following regex for commafication:

/(\d)(?=(\d\d\d)+(?!\d))/$1,/

Haven’t tried look ahead in Ruby but I think this may work for you,  
perhaps with some special handling of the “cents” portion of the  
amount.

Ken
