putting commas in floats to seperate hundreds from thousands etc...

J

John Kopanas

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 Kopanas
(e-mail address removed)

http://www.kopanas.com
http://www.cusec.net
http://www.soen.info
 
C

coachhilton

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,578
Members
45,052
Latest member
LucyCarper

Latest Threads

Top