Forum: Ruby Formatted output for numbers with comma's?

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.
D948f30e520e8e8545e1877d77e56b74?d=identicon&s=25 Wink Saville (Guest)
on 2006-03-14 09:14
(Received via mailing list)
I was looking for a routine to convert a Number to a string with
comma's, is there a routine in the library for that?

Wink
9dfe8c734b0f9b37a4e218425c0a2138?d=identicon&s=25 Gene Tani (Guest)
on 2006-03-14 09:44
(Received via mailing list)
Wink Saville wrote:
> I was looking for a routine to convert a Number to a string with
> comma's, is there a routine in the library for that?
>


http://rubyforge.org/snippet/detail.php?type=snipp...

http://extensions.rubyforge.org/rdoc/classes/Numer...
http://sourceforge.net/projects/ruby-locale/

http://globalize-rails.org/wiki/
4299e35bacef054df40583da2d51edea?d=identicon&s=25 James Gray (bbazzarrakk)
on 2006-03-14 14:49
(Received via mailing list)
On Mar 14, 2006, at 2:11 AM, Wink Saville wrote:

> I was looking for a routine to convert a Number to a string with
> comma's, is there a routine in the library for that?

It's not very hard to roll up a solution:

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

Hope that helps.

James Edward Gray II
E2b3bbf241a5daee3ee7696a7d4b2015?d=identicon&s=25 Vrtwo Lastname (d0t1q)
on 2006-03-15 02:28
(Received via mailing list)
This would be a nice (ruby builtin) method :-)


  def commify( number, positions)
    positions = 3 if positions == nil
    puts number.to_s.reverse.gsub(/(\d\d\d)(?=\d)(?!\d*\.)/,
'\1,').reverse
      ^ not very sure how it would be done but something similar to
("\d"*positions)
  end

  puts commify(12345) # 12,345
  puts commify(12345, 2) # 1,23,45
Eec967b23f805cdbf09eeb8095fdaade?d=identicon&s=25 SteveC (Guest)
on 2006-03-20 00:31
(Received via mailing list)
* @ 15/03/06 01:27:40 AM caldridge@gmail.com wrote:
> This would be a nice (ruby builtin) method :-)

+1 vote as I've needed it quite a bit.

How would I/we go about getting something like this in stdlib?

have fun,

SteveC steve@asklater.com http://www.asklater.com/steve/
31ab75f7ddda241830659630746cdd3a?d=identicon&s=25 Austin Ziegler (Guest)
on 2006-03-20 01:11
(Received via mailing list)
On 3/14/06, James Edward Gray II <james@grayproductions.net> wrote:
> On Mar 14, 2006, at 2:11 AM, Wink Saville wrote:
> > I was looking for a routine to convert a Number to a string with
> > comma's, is there a routine in the library for that?
>
> It's not very hard to roll up a solution:
>
>    def commify( number )
>      number.to_s.reverse.gsub(/(\d\d\d)(?=\d)(?!\d*\.)/, '\1,').reverse
>    end

...it's significantly harder to do it completely *right*, though.

See Gavin Sinclair's Extensions library in Numeric. I have a
relatively complete method written there, done in test-first style.

-austin
This topic is locked and can not be replied to.