Forum: Ruby Formatting to "Thousands"

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
896f7da88fbde8fa5f3519c890163e17?d=identicon&s=25 Barrie Jarman (Guest)
on 2006-02-28 12:09
(Received via mailing list)
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...
4299e35bacef054df40583da2d51edea?d=identicon&s=25 James Gray (bbazzarrakk)
on 2006-02-28 14:54
(Received via mailing list)
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
12a71a456ac3d464914a8267f11d43b3?d=identicon&s=25 semmons99@gmail.com (Guest)
on 2006-02-28 17:16
(Received via mailing list)
I have to say, that is some great Regex Fu. I was looking to do
something similar but your solution is much nicer!
896f7da88fbde8fa5f3519c890163e17?d=identicon&s=25 Barrie Jarman (Guest)
on 2006-02-28 17:50
(Received via mailing list)
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...
149379873fe2cb70e550c6bff8fedd0c?d=identicon&s=25 Jeffrey Schwab (Guest)
on 2006-02-28 17:56
(Received via mailing list)
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
4299e35bacef054df40583da2d51edea?d=identicon&s=25 James Gray (bbazzarrakk)
on 2006-02-28 17:59
(Received via mailing list)
On Feb 28, 2006, at 10:53 AM, Jeffrey Schwab wrote:

>   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
C8a634a01a2c4508360874bff7fb1a7f?d=identicon&s=25 Kevin Olbrich (olbrich)
on 2006-02-28 18:05
(Received via mailing list)
Or you could just use 'gsub' instead of 'gsub!' and avoid the nil

_Kevin
E34b5cae57e0dd170114dba444e37852?d=identicon&s=25 Logan Capaldo (Guest)
on 2006-03-01 04:57
(Received via mailing list)
On Feb 28, 2006, at 11:59 AM, James Edward Gray II wrote:

>> performed, e.g. if the number has fewer than 4 digits.  This can
>>     s.gsub!(/(\d\d\d)(?=\d)(?!\d*\.)/, '\1,')
>
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 ;)
149379873fe2cb70e550c6bff8fedd0c?d=identicon&s=25 Jeffrey Schwab (Guest)
on 2006-03-01 17:23
(Received via mailing list)
Logan Capaldo wrote:
>>>
>>>   def commify(number)
>> Thanks.
> 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. :)
This topic is locked and can not be replied to.