Browse | Submit A New Snippet | Create A Package

 

Commify a number

Type:
Sample Code (HOWTO)
Category:
Math Functions
License:
Ruby License
Language:
Ruby
 
Description:
Commifies a number. Preserved from Warren Brown's ruby-talk posting here - http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/78103.

Versions Of This Snippet::

Trejkaz
Snippet ID Download Version Date Posted Author Delete
5112.22009-09-20 23:14Trejkaz
Changes since last version::
2.1 was broken for numbers < 1000 so I'm reverting to 2.0 behaviour.
4712.12009-05-05 15:32Robert Klemme
Changes since last version::
Less replacements
112.02003-08-05 18:21Tom Copeland
Changes since last version::
Works with more decimal points now.
101.02003-08-05 17:10Tom Copeland

Download a raw-text version of this code by clicking on "Download Version"

 


Latest Snippet Version: :2.2

n.to_s.gsub(/(\d)(?=\d{3}+(?:\.|$))(\d{3}\..*)?/,'\1,\2')

		

Submit a new version

You can submit a new version of this snippet if you have modified it and you feel it is appropriate to share with others..