[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Formatting Numbers

Hunter's Lists

2/9/2006 1:44:00 AM

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




10 Answers

Logan Capaldo

2/9/2006 2:11:00 AM

0


On Feb 8, 2006, at 8:43 PM, HH wrote:

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



Kent Sibilev

2/9/2006 2:26:00 AM

0

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

On 2/8/06, Logan Capaldo <logancapaldo@gmail.com> wrote:
>
> On Feb 8, 2006, at 8:43 PM, HH wrote:
>
> > 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"
>
>
>


David Vallner

2/9/2006 2:28:00 AM

0

Dna Štvrtok 09 Február 2006 03:11 Logan Capaldo napísal:
> On Feb 8, 2006, at 8:43 PM, HH wrote:
> > 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"

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


Logan Capaldo

2/9/2006 2:43:00 AM

0


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



konsu

2/9/2006 3:27:00 AM

0

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


James Gray

2/9/2006 3:48:00 AM

0

On Feb 8, 2006, at 7:43 PM, HH wrote:

> 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


Gene Tani

2/9/2006 3:50:00 AM

0


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/Numeric.ht...

Hunter's Lists

2/9/2006 4:59:00 AM

0

Thanks to everyone for all the great suggestions.

I really appreciate it.

> From: Gene Tani <gene.tani@gmail.com>
> Organization: http://groups....
> Reply-To: <ruby-talk@ruby-lang.org>
> Newsgroups: comp.lang.ruby
> Date: Thu, 9 Feb 2006 12:53:22 +0900
> To: ruby-talk ML <ruby-talk@ruby-lang.org>
> Subject: Re: Formatting Numbers
>
>
> 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/Numeric.ht...
>
>




David Vallner

2/9/2006 1:55:00 PM

0

Hmm.

http://sourceforge.net/projects/ru...

Anyone have any experience with that?

David Vallner

Dna Štvrtok 09 Február 2006 04:28 konsu napísal:
> 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...
>


BeamMeUpScotty

8/10/2012 2:45:00 PM

0

On 8/9/2012 11:15 PM, Bret Cahill wrote:
>> Please explain to me why I should be mad at what Mitt does with HIS money.
>> And why I SHOULDN'T be mad at what obuma does with MY MONEY !!!
>
> You need to take personal responsibility for your monetary losses.
> You don't have to give any of your money to anyone. Call 1-800-FLY-4-
> LESS and book the next one way flight to Mogadishu in low tax paradise
> Somalia.
>
>
> Bret Cahill

If Bill gates and enough others that really do excel at something are
willing to take you up on that offer, you'll be here starving in
communism and Somalia will be pulled into being a superpower by the
capitalism and freedom.


--

*Rumination*

#8 - It's NOT what you earn it's what you keep.