#!/usr/bin/env ruby require 'trollop' cfg = Trollop::options do banner "avg < " opt :round, "Number of digits after decimal point.", :type => :int, :default => -1 end sum = 0.0 i = 0 while line = STDIN.gets sum += line.to_f i +=1 end avg = sum/i.to_f if cfg[:round] >= 0 puts avg.round cfg[:round] else puts avg end