[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

Formatting to "Thousands"

Barrie Jarman

2/28/2006 11:11:00 AM

A colleague of mine wishes to format a float to include commas at thousand
values, for example
12345.21 becomes
12,345.21

Is there any inbuilt functionality to format this, or are we better of
writing something ourselves...






--
Posted via NewsDemon.com - Premium Uncensored Newsgroup Service
------->>>>>>http://www.NewsDemon.com<<<<<...
Unlimited Access, Anonymous Accounts, Uncensored Broadband Access
8 Answers

James Gray

2/28/2006 1:54:00 PM

0

On Feb 28, 2006, at 5:08 AM, Barrie Jarman wrote:

> A colleague of mine wishes to format a float to include commas at
> thousand
> values, for example
> 12345.21 becomes
> 12,345.21
>
> Is there any inbuilt functionality to format this, or are we better of
> writing something ourselves...

It's not built-in, but it's also pretty easy to roll one:

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

Hope that helps.

James Edward Gray II



Shane Emmons

2/28/2006 4:11:00 PM

0

I have to say, that is some great Regex Fu. I was looking to do
something similar but your solution is much nicer!

Barrie Jarman

2/28/2006 4:50:00 PM

0

Implemented this into the float class.
Adds check to make sure 0 is handled properly and adds for two decimal
places (sorry, probably wasn't specific enough before).

def to_finance
if self != 0
number = sprintf("%.2f", self)
number.to_s.reverse.gsub!(/(\d\d\d)(?=\d)(?!\d*\.)/, '\1,').reverse
else
sprintf("%.2f", self)
end
end

"James Edward Gray II" <james@grayproductions.net> wrote in message
news:47AC1DDA-5D15-461F-AEB2-40B5E23903B1@grayproductions.net...
> On Feb 28, 2006, at 5:08 AM, Barrie Jarman wrote:
>
>> A colleague of mine wishes to format a float to include commas at
>> thousand
>> values, for example
>> 12345.21 becomes
>> 12,345.21
>>
>> Is there any inbuilt functionality to format this, or are we better of
>> writing something ourselves...
>
> It's not built-in, but it's also pretty easy to roll one:
>
> def commify( number )
> number.to_s.reverse.gsub!(/(\d\d\d)(?=\d)(?!\d*\.)/, '\1,').reverse
> end
>
> Hope that helps.
>
> James Edward Gray II
>
>
>



--
Posted via NewsDemon.com - Premium Uncensored Newsgroup Service
------->>>>>>http://www.NewsDemon.com<<<<<...
Unlimited Access, Anonymous Accounts, Uncensored Broadband Access

Jeff Schwab

2/28/2006 4:53:00 PM

0

James Edward Gray II wrote:

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

That's nice, except that gsub! returns nil if no substitutions are
performed, e.g. if the number has fewer than 4 digits. This can result
in an exception like:

main.rb:3:in `commify':
undefined method `reverse' for nil:NilClass(NoMethodError)

Of course, you could just break the chain across multiple lines:

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

James Gray

2/28/2006 5:00:00 PM

0

On Feb 28, 2006, at 10:53 AM, Jeffrey Schwab wrote:

> James Edward Gray II wrote:
>
>> def commify( number )
>> number.to_s.reverse.gsub!(/(\d\d\d)(?=\d)(?!\d*\.)/, '\1,').reverse
>> end
>
> That's nice, except that gsub! returns nil if no substitutions are
> performed, e.g. if the number has fewer than 4 digits. This can
> result in an exception like:
>
> main.rb:3:in `commify':
> undefined method `reverse' for nil:NilClass(NoMethodError)
>
> Of course, you could just break the chain across multiple lines:
>
> def commify(number)
> s = number.to_s
> s.reverse!
> s.gsub!(/(\d\d\d)(?=\d)(?!\d*\.)/, '\1,')
> s.reverse!
> end

Good catch. I originally had it in multiple lines and I forgot to
remove the ! when I shortened it for this post.

Thanks.

James Edward Gray II



Kevin Olbrich

2/28/2006 5:03:00 PM

0

Or you could just use 'gsub' instead of 'gsub!' and avoid the nil

_Kevin

-----Original Message-----
From: Jeffrey Schwab [mailto:jeff@schwabcenter.com]
Sent: Tuesday, February 28, 2006 11:54 AM
To: ruby-talk ML
Subject: Re: Formatting to "Thousands"

James Edward Gray II wrote:

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

That's nice, except that gsub! returns nil if no substitutions are
performed, e.g. if the number has fewer than 4 digits. This can result in
an exception like:

main.rb:3:in `commify':
undefined method `reverse' for nil:NilClass(NoMethodError)

Of course, you could just break the chain across multiple lines:

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



Logan Capaldo

3/1/2006 3:57:00 AM

0


On Feb 28, 2006, at 11:59 AM, James Edward Gray II wrote:

> On Feb 28, 2006, at 10:53 AM, Jeffrey Schwab wrote:
>
>> James Edward Gray II wrote:
>>
>>> def commify( number )
>>> number.to_s.reverse.gsub!(/(\d\d\d)(?=\d)(?!\d*\.)/,
>>> '\1,').reverse
>>> end
>>
>> That's nice, except that gsub! returns nil if no substitutions are
>> performed, e.g. if the number has fewer than 4 digits. This can
>> result in an exception like:
>>
>> main.rb:3:in `commify':
>> undefined method `reverse' for nil:NilClass(NoMethodError)
>>
>> Of course, you could just break the chain across multiple lines:
>>
>> def commify(number)
>> s = number.to_s
>> s.reverse!
>> s.gsub!(/(\d\d\d)(?=\d)(?!\d*\.)/, '\1,')
>> s.reverse!
>> end
>
> Good catch. I originally had it in multiple lines and I forgot to
> remove the ! when I shortened it for this post.
>
> Thanks.
>
> James Edward Gray II
>
>

I don't know why but using regexps to do this kind of scares me.
Maybe its cause I don't have as much regexp fu but it just seems like
ruby
ruby
perl
ruby
ruby

Not that I have anything against perl or regexps, but that particular
regexp seems to scream "I'm doing this with a regexp not because it's
easier but because it's faster and shorter." Not that that's wrong.
Hmm I seem to be trying really hard not to offend anyone :-p. I guess
a rule of thumb for me is more than 1 lookahead assertion and my eyes
glaze over ;)



Jeff Schwab

3/1/2006 4:23:00 PM

0

Logan Capaldo wrote:
>
> On Feb 28, 2006, at 11:59 AM, James Edward Gray II wrote:
>
>> On Feb 28, 2006, at 10:53 AM, Jeffrey Schwab wrote:
>>
>>> James Edward Gray II wrote:
>>>
>>>> def commify( number )
>>>> number.to_s.reverse.gsub!(/(\d\d\d)(?=\d)(?!\d*\.)/, '\1,').reverse
>>>> end
>>>
>>>
>>> That's nice, except that gsub! returns nil if no substitutions are
>>> performed, e.g. if the number has fewer than 4 digits. This can
>>> result in an exception like:
>>>
>>> main.rb:3:in `commify':
>>> undefined method `reverse' for nil:NilClass(NoMethodError)
>>>
>>> Of course, you could just break the chain across multiple lines:
>>>
>>> def commify(number)
>>> s = number.to_s
>>> s.reverse!
>>> s.gsub!(/(\d\d\d)(?=\d)(?!\d*\.)/, '\1,')
>>> s.reverse!
>>> end
>>
>>
>> Good catch. I originally had it in multiple lines and I forgot to
>> remove the ! when I shortened it for this post.
>>
>> Thanks.
>>
>> James Edward Gray II
>>
>>
>
> I don't know why but using regexps to do this kind of scares me. Maybe
> its cause I don't have as much regexp fu but it just seems like
> ruby
> ruby
> perl
> ruby
> ruby
>
> Not that I have anything against perl or regexps, but that particular
> regexp seems to scream "I'm doing this with a regexp not because it's
> easier but because it's faster and shorter." Not that that's wrong. Hmm
> I seem to be trying really hard not to offend anyone :-p. I guess a
> rule of thumb for me is more than 1 lookahead assertion and my eyes
> glaze over ;)

Try the Friedl book. It will open your eyes. :)