Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > format number with comma separators?

Reply
Thread Tools

format number with comma separators?

 
 
Chris Morris
Guest
Posts: n/a
 
      08-05-2003
I'm brain dead and just trying to get formatted numbers in a task that's
already 10 tangents deep -- argh. Anyway, how can I sprintf (or
otherwise) this:

456778904

to this:

456,778,904

--

Chris
http://clabs.org/blogki



 
Reply With Quote
 
 
 
 
Mark J. Reed
Guest
Posts: n/a
 
      08-05-2003
On Tue, Aug 05, 2003 at 11:55:12PM +0900, Chris Morris wrote:
> I'm brain dead and just trying to get formatted numbers in a task that's
> already 10 tangents deep -- argh. Anyway, how can I sprintf (or
> otherwise) this:
>
> 456778904
>
> to this:
>
> 456,778,904


Unless there's something I don't know about - a distinct possibility
- there's no built-in function to do this. You can, however,
do it with a regex. Assuming the numbers are all integers (no
decimal points), then this will work:

formatted_n = n.to_s.reverse.gsub(/...(?=.)/,'\&,').reverse

-Mark
 
Reply With Quote
 
 
 
 
Robert Klemme
Guest
Posts: n/a
 
      08-05-2003

"Mark J. Reed" <(E-Mail Removed)> schrieb im Newsbeitrag
news:(E-Mail Removed)...
> On Tue, Aug 05, 2003 at 11:55:12PM +0900, Chris Morris wrote:
> > I'm brain dead and just trying to get formatted numbers in a task

that's
> > already 10 tangents deep -- argh. Anyway, how can I sprintf (or
> > otherwise) this:
> >
> > 456778904
> >
> > to this:
> >
> > 456,778,904

>
> Unless there's something I don't know about - a distinct possibility
> - there's no built-in function to do this. You can, however,
> do it with a regex. Assuming the numbers are all integers (no
> decimal points), then this will work:
>
> formatted_n = n.to_s.reverse.gsub(/...(?=.)/,'\&,').reverse


This fails for negative numbers in the range -100..-999 and all other
negative numbers with an amount of digits that is divisable by 3.

Alternative:

def format(num)
s = num.to_s

if s.include? ?.
pre, post = s.split '.'
"#{pre.reverse.gsub( /\d{3}(?=\d)/, '\&,' ).reverse}.#{post}"
else
s.reverse.gsub( /\d{3}(?=\d)/, '\&,' ).reverse
end
end

robert

 
Reply With Quote
 
Robert Klemme
Guest
Posts: n/a
 
      08-05-2003


"Chris Morris" <(E-Mail Removed)> schrieb im Newsbeitrag
news:(E-Mail Removed)...
> Chris Morris wrote:
>
> > I'm brain dead and just trying to get formatted numbers in a task
> > that's already 10 tangents deep -- argh. Anyway, how can I sprintf (or
> > otherwise) this:
> >
> > 456778904
> >
> > to this:
> >
> > 456,778,904
> >

> Found this: http://www.rubygarden.org/ruby?FixNumFormat


Due to probs with negative numbers I added another version to that.

robert

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: Insert comma in number? John Posner Python 0 03-07-2013 07:06 PM
Insert comma in number? eli m Python 6 03-07-2013 08:58 AM
multiline CSV records (comma-separated values format) Yakov Perl Misc 2 05-12-2007 10:39 PM
converting exponential format number to decimal format number Fei Liu Perl Misc 21 12-16-2006 01:49 AM
How to format $mail_from with 2 variables separated by a comma? Jason Miles Perl 1 05-28-2004 02:48 AM



Advertisments