format number with comma separators?

Discussion in 'Ruby' started by Chris Morris, Aug 5, 2003.

  1. Chris Morris

    Chris Morris Guest

    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
     
    Chris Morris, Aug 5, 2003
    #1
    1. Advertising

  2. Chris Morris

    Mark J. Reed Guest

    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
     
    Mark J. Reed, Aug 5, 2003
    #2
    1. Advertising

  3. "Mark J. Reed" <> schrieb im Newsbeitrag
    news:...
    > 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
     
    Robert Klemme, Aug 5, 2003
    #3
  4. "Chris Morris" <> schrieb im Newsbeitrag
    news:...
    > 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
     
    Robert Klemme, Aug 5, 2003
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Edward Grosso via .NET 247

    Save a Recordset to an ASCII file with custom field separators

    Edward Grosso via .NET 247, Jun 11, 2004, in forum: ASP .Net
    Replies:
    3
    Views:
    465
    David Browne
    Jun 11, 2004
  2. Ulf Meinhardt
    Replies:
    1
    Views:
    531
    J. David Boyd
    Mar 17, 2005
  3. Ian Pilcher
    Replies:
    0
    Views:
    366
    Ian Pilcher
    Feb 10, 2006
  4. dorayme

    inline list separators

    dorayme, Nov 6, 2006, in forum: HTML
    Replies:
    35
    Views:
    1,297
    BootNic
    Nov 9, 2006
  5. Fei Liu
    Replies:
    21
    Views:
    2,485
    John Bokma
    Dec 16, 2006
Loading...

Share This Page