format number with comma separators?

Ask a Question related to Ruby, Design and Development.

  1. #1

    Default format number with comma separators?

    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
    [url]http://clabs.org/blogki[/url]



    Chris Morris Guest

  2. Similar Questions and Discussions

    1. Format Phone Number
      I have a text feild (homephone) in MSAccess that hold phone numbers (410)543-3333 When I display these in a <CFOUTPUT>#HOMEPHONE#<\CFOUTPUT> the...
    2. Format a number on SELECT
      I know Fusion has a function called NumberFormat() for formatting numbers on the fusion side during display, but I was wondering if I can select the...
    3. How to format Integer to include comma
      which function do I use to format an integer value to include a comma? Or is there a currency function that will take 2500 and output $2,500?
    4. Number Format
      Hi, In the FrontPage Database Wizard I have the following custom query: Select *, FORMAT(Number, '#.00') AS fNumber FROM MyFile When I view...
    5. format number into currency format ($1,000,000.00)
      SELECT '$' + CONVERT(VARCHAR, CONVERT(MONEY, 1000000), 1) "lamP" <phantlam@yahoo.com> wrote in message...
  3. #2

    Default Re: format number with comma separators?

    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 Guest

  4. #3

    Default Re: format number with comma separators?


    "Mark J. Reed" <markjreed@mail.com> schrieb im Newsbeitrag
    news:20030805150600.GD28141@mulan.thereeds.org...
    > 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 Guest

  5. #4

    Default Re: format number with comma separators?



    "Chris Morris" <chrismo@clabs.org> schrieb im Newsbeitrag
    news:3F2FCA9F.4000600@clabs.org...
    > 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: [url]http://www.rubygarden.org/ruby?FixNumFormat[/url]
    Due to probs with negative numbers I added another version to that.

    robert

    Robert Klemme Guest

  6. #5

    Default Re: format number with comma separators?

    On Tue, 2003-08-05 at 12:07, Warren Brown wrote:
    > If you want to allow a decimal point:
    >
    > irb(main):001:0> puts "-123456789.01".gsub(/(\d)(?=\d{3}+(\.\d*)?$)/, '\1,')
    > -123,456,789.01
    >
    > - Warren Brown
    Immortalized here:

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

    Yours,

    tom



    Tom Copeland Guest

  7. #6

    Default Re: format number with comma separators?

    On Tuesday 05 August 2003 18:07, Warren Brown wrote:
    > >> Yet another regex solution:
    > >>
    > >> % ruby -e 'puts "456778904".gsub(/(.)(?=.{3}+$)/, %q(\1,))'
    > >> 456,778,904
    > >>
    > >> -- fxn
    > >>
    > >> .gsub(/(\d)(?=\d{3}+$)/, '\1,')
    > >
    > > That spurious line at the end uses a regex that works for negative
    > > numbers as well.
    >
    > If you want to allow a decimal point:
    >
    > irb(main):001:0> puts "-123456789.01".gsub(/(\d)(?=\d{3}+(\.\d*)?$)/,
    > '\1,') -123,456,789.01
    But:

    irb(main):003:0> puts "-123456789.0123".gsub(/(\d)(?=\d{3}+(\.\d*)?$)/, '\1,')
    -123,456,789.0,123

    Is that the expected behaviour? Which is the convention in the decimal part?

    -- fxn


    Xavier Noria Guest

  8. #7

    Default Re: format number with comma separators?

    > irb(main):001:0> puts
    > "-123456789.01".gsub(/(\d)(?=\d{3}+(\.\d*)?$)/,'\1,')
    > -123,456,789.01
    Whoops,

    irb(main):001:0> puts
    "-123456789.012345".gsub(/(\d)(?=\d{3}+(\.\d*)?$)/,'\1,')
    -123,456,789.012,345


    How about:

    irb(main):028:0> puts
    "-123456789.123456789".gsub(/(\d)(?=\d{3}+(?:\.|$))(\d{3}\..*)?/,'\1,\2')
    -123,456,789.123456789

    - Warren Brown


    Warren Brown Guest

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139