midwire
8/19/2014 - 1:11 AM

Better Ruby Float class

Better Ruby Float class

class Float
  # Format a number with commas and a decimal point
  def commify
    to_s =~ /([^\.]*)(\..*)?/
    int, dec = $1.reverse, $2 ? $2 : ""
    while int.gsub!(/(,|\.|^)(\d{3})(\d)/, '\1\2,\3')
    end
    int.reverse + dec
  end
end