This browser does not support basic Web standards, preventing the display of our site's intended design. May we suggest that you upgrade your browser?

Web Development Code Library RubyComma-format numbers

Comma-format numbers

Ruby implementation of the well-known comma regular expression. format_with_commas 1222343561234 would return "1,222,343,561,234"

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

end
Excellence. Our Measure. Our Motto. Our Goal.