Formatting Numbers

H

HH

This has got to be simple but I checked the PickAxe and Google to no avail.
My guess is that I'm looking for it in terms that don't make sense.

Anyway...

I have a fixnum:

a = 1000

I would like to output it with a comma, such as "1,000". Is there a
formatting class or something I can use something along these lines
(thinking from my Java background):

Formatter f = Formatter.new("N0")
puts f(a)

Any help appreciated.

Thanks,
Hunter
 
L

Logan Capaldo

This has got to be simple but I checked the PickAxe and Google to
no avail.
My guess is that I'm looking for it in terms that don't make sense.

Anyway...

I have a fixnum:

a = 1000

I would like to output it with a comma, such as "1,000". Is there a
formatting class or something I can use something along these lines
(thinking from my Java background):

Formatter f = Formatter.new("N0")
puts f(a)

Any help appreciated.

Thanks,
Hunter

I dunno know about some kind of builtin format object (I imagine
maybe the right incantation of sprintf could do it) but heres a
method (kinda long for what it is, theres probably some regex to do
it in one fell swoop):

% cat commify.rb
require 'enumerator'
def commify(num)
str = num.to_s
a = []
str.split(//).reverse.each_slice(3) { |slice| a << slice }
new_a = []
a.each do |item|
new_a << item
new_a << [","]
end
new_a.delete_at(new_a.length - 1)
new_a.flatten.reverse.join
end

p commify(1000)
p commify(100)
p commify(1_000_000)
p commify(1024)

% ruby commify.rb
"1,000"
"100"
"1,000,000"
"1,024"
 
K

Kent Sibilev

Or

irb(main):001:0> class Integer
irb(main):002:1> def commify
irb(main):003:2> self.to_s.gsub(/(\d)(?=3D(\d{3})+$)/,'\1,')
irb(main):004:2> end
irb(main):005:1> end
=3D> nil
irb(main):006:0> 1000.commify
=3D> "1,000"
irb(main):007:0> 100.commify
=3D> "100"
irb(main):008:0> 1_000_000.commify
=3D> "1,000,000"
irb(main):009:0> 1024.commify
=3D> "1,024"

Kent

This has got to be simple but I checked the PickAxe and Google to
no avail.
My guess is that I'm looking for it in terms that don't make sense.

Anyway...

I have a fixnum:

a =3D 1000

I would like to output it with a comma, such as "1,000". Is there a
formatting class or something I can use something along these lines
(thinking from my Java background):

Formatter f =3D Formatter.new("N0")
puts f(a)

Any help appreciated.

Thanks,
Hunter

I dunno know about some kind of builtin format object (I imagine
maybe the right incantation of sprintf could do it) but heres a
method (kinda long for what it is, theres probably some regex to do
it in one fell swoop):

% cat commify.rb
require 'enumerator'
def commify(num)
str =3D num.to_s
a =3D []
str.split(//).reverse.each_slice(3) { |slice| a << slice }
new_a =3D []
a.each do |item|
new_a << item
new_a << [","]
end
new_a.delete_at(new_a.length - 1)
new_a.flatten.reverse.join
end

p commify(1000)
p commify(100)
p commify(1_000_000)
p commify(1024)

% ruby commify.rb
"1,000"
"100"
"1,000,000"
"1,024"
 
D

David Vallner

D=C5=88a =C5=A0tvrtok 09 Febru=C3=A1r 2006 03:11 Logan Capaldo nap=C3=ADsal:
This has got to be simple but I checked the PickAxe and Google to
no avail.
My guess is that I'm looking for it in terms that don't make sense.

Anyway...

I have a fixnum:

a =3D 1000

I would like to output it with a comma, such as "1,000". Is there a
formatting class or something I can use something along these lines
(thinking from my Java background):

Formatter f =3D Formatter.new("N0")
puts f(a)

Any help appreciated.

Thanks,
Hunter

I dunno know about some kind of builtin format object (I imagine
maybe the right incantation of sprintf could do it) but heres a
method (kinda long for what it is, theres probably some regex to do
it in one fell swoop):

% cat commify.rb
require 'enumerator'
def commify(num)
str =3D num.to_s
a =3D []
str.split(//).reverse.each_slice(3) { |slice| a << slice }
new_a =3D []
a.each do |item|
new_a << item
new_a << [","]
end
new_a.delete_at(new_a.length - 1)
new_a.flatten.reverse.join
end

p commify(1000)
p commify(100)
p commify(1_000_000)
p commify(1024)

% ruby commify.rb
"1,000"
"100"
"1,000,000"
"1,024"

Hmm, I was in the middle of writing precisely this sort of hack as this=20
arrived. That said, much better job here. The #each_slice comes from the=20
enumerator library? I should really learn to use that one...

That said, I'm almost sure you can golf that thing down a bit. How about:

require 'enumerator'
def commify(num)
str =3D num.to_s
a =3D []
str.split(//).reverse.each_slice(3) { |slice| a << slice }
a.reverse.collect{|i| i.reverse.join}.join(",")
end

p commify(1000)
p commify(100)
p commify(1_000_000)
p commify(1024)

Now if only we directly had a method to slice an Array into an Array of=20
slices, the whole thing could turn into a one-liner. Which I hate, but in a=
=20
good way.

David Vallner
 
L

Logan Capaldo

Now if only we directly had a method to slice an Array into an
Array of
slices, the whole thing could turn into a one-liner. Which I hate,
but in a
good way.

David Vallner

str.split(//).reverse.to_enum:)each_slice,3).to_a
 
K

konsu

hello,

there is even bigger problem: the formatting that you are looking for
depends on locale, and as far as i could tell ruby does not provide any
support for that. so the solutions that people suggested would work only for
american formatting, and as soon as you choose to change your formatting and
use comma for decimal point you are at the square one...

konstantin
 
J

James Edward Gray II

This has got to be simple but I checked the PickAxe and Google to
no avail.
My guess is that I'm looking for it in terms that don't make sense.

Anyway...

I have a fixnum:

a = 1000

I would like to output it with a comma, such as "1,000". Is there a
formatting class or something I can use something along these lines
(thinking from my Java background):

Formatter f = Formatter.new("N0")
puts f(a)

Any help appreciated.

def commify( str )
str.reverse.gsub(/(\d\d\d)(?=\d)(?!\d*\.)/,'\1,').reverse
end

Handles floats too.

James Edward Gray II
 
M

Michael Nissim

Logan said:
str.split(//).reverse.to_enum:)each_slice,3).to_a

The one above from Logan didn't work for me.

But this one from James Edward Gray II did:
str.reverse.gsub(/(\d\d\d)(?=\d)(?!\d*\.)/,'\1,').reverse

Thanks to both.
 

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
474,148
Messages
2,570,838
Members
47,385
Latest member
Joneswilliam01

Latest Threads

Top