def format_comma(n) s = '' until n < 1000 n,m = n.divmod(1000) s = ",#{'%03d' % m}#{s}" end "#{n}#{s}" end puts format_comma(gets.to_i)