Forum: Ruby Formatting Numbers

A7db9ec803b5895ae5f916a74e2db329?d=identicon&s=25 HH (Guest)
on 2006-02-09 02:43
(Received via mailing list)
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
E34b5cae57e0dd170114dba444e37852?d=identicon&s=25 Logan Capaldo (Guest)
on 2006-02-09 03:12
(Received via mailing list)
On Feb 8, 2006, at 8:43 PM, HH wrote:

> I would like to output it with a comma, such as "1,000". Is there a
>
>
>

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"
3ccecc71b9fb0a3d7f00a0bef6f0a63a?d=identicon&s=25 Kent Sibilev (Guest)
on 2006-02-09 03:25
(Received via mailing list)
Or

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

Kent
430ea1cba106cc65b7687d66e9df4f06?d=identicon&s=25 David Vallner (Guest)
on 2006-02-09 03:27
(Received via mailing list)
DÅ?a Å tvrtok 09 Február 2006 03:11 Logan Capaldo napísal:
> >
> > Hunter
>    a = []
> 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
arrived. That said, much better job here. The #each_slice comes from the
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 =  num.to_s
		a = []
		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
slices, the whole thing could turn into a one-liner. Which I hate, but
in a
good way.

David Vallner
E34b5cae57e0dd170114dba444e37852?d=identicon&s=25 Logan Capaldo (Guest)
on 2006-02-09 03:43
(Received via mailing list)
On Feb 8, 2006, at 9:27 PM, David Vallner wrote:
>
> 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
B39c2415f51b64da5fdc5637a2e81a89?d=identicon&s=25 konsu (Guest)
on 2006-02-09 04:28
(Received via mailing list)
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

"HH" <lists@lastonepicked.com> wrote in message
news:C00FDE38.BE9B2%lists@lastonepicked.com...
4299e35bacef054df40583da2d51edea?d=identicon&s=25 James Gray (bbazzarrakk)
on 2006-02-09 04:47
(Received via mailing list)
On Feb 8, 2006, at 7:43 PM, HH wrote:

> 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
9dfe8c734b0f9b37a4e218425c0a2138?d=identicon&s=25 Gene Tani (Guest)
on 2006-02-09 04:53
(Received via mailing list)
HH wrote:
>
> 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):
>

http://extensions.rubyforge.org/rdoc/classes/Numer...
A7db9ec803b5895ae5f916a74e2db329?d=identicon&s=25 HH (Guest)
on 2006-02-09 05:58
(Received via mailing list)
Thanks to everyone for all the great suggestions.

I really appreciate it.
430ea1cba106cc65b7687d66e9df4f06?d=identicon&s=25 David Vallner (Guest)
on 2006-02-09 15:28
(Received via mailing list)
Hmm.

http://sourceforge.net/projects/ruby-locale/

Anyone have any experience with that?

David Vallner

DÅ?a Å tvrtok 09 Február 2006 04:28 konsu napísal:
Fc87c7e3fd2b45d0bdd35bc9ad3fdc7d?d=identicon&s=25 Michael Nissim (mjnissim)
on 2010-07-04 11:07
Logan Capaldo wrote:
> On Feb 8, 2006, at 9:27 PM, David Vallner wrote:
>>
>> 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

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.
Please log in before posting. Registration is free and takes only a minute.
Existing account

NEW: Do you have a Google/GoogleMail, Yahoo or Facebook account? No registration required!
Log in with Google account | Log in with Yahoo account | Log in with Facebook account
No account? Register here.