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

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

···

Chris
http://clabs.org/blogki

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

···

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

Yet another regex solution:

% ruby -e 'puts "456778904".gsub(/(.)(?=.{3}+$)/, %q(\1,))'
456,778,904

– fxn

.gsub(/(\d)(?=\d{3}+$)/, '\1,')
···

On Tuesday 05 August 2003 16:55, 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

“Mark J. Reed” markjreed@mail.com schrieb im Newsbeitrag
news:20030805150600.GD28141@mulan.thereeds.org

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
···

On Tue, Aug 05, 2003 at 11:55:12PM +0900, Chris Morris wrote:

That spurious line at the end uses a regex that works for negative
numbers as well.

– fxn

···

On Tuesday 05 August 2003 17:32, Xavier Noria wrote:

Yet another regex solution:

% ruby -e 'puts "456778904".gsub(/(.)(?=.{3}+$)/, %q(\1,))'
456,778,904

– fxn

.gsub(/(\d)(?=\d{3}+$)/, '\1,')

“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: http://www.rubygarden.org/ruby?FixNumFormat

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

robert

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

- Warren Brown

I’ve created a “very” robust version that can handle differing separators,
decimal points, digit group sizes, and negative formats. It also supports
grouping the fractional part of a decimal number. Note that the way that I
have written this, a value of “nil” for any value will produce the default
value. Thus, you can do:

12345.5555.format_s(nil, nil, nil, nil,true) # => 12,345.555,5

There is a full set of unit tests for this.

I’ve posted it at: http://www.rubygarden.org/ruby?FixNumFormat

-austin

···


austin ziegler * austin@halostatue.ca * Toronto, ON, Canada
software designer * pragmatic programmer * 2003.08.05
* 12.52.26

Immortalized here:

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

Yours,

tom

···

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

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

···

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

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