UNITS = %w[kbps Mbps Gbps Tbps] def from_human(bps, unit) (UNITS.size+1).times do |round| return bps unless UNITS[round..-1].include?(unit) bps *= 1000 end end def to_human(bps) unit = "bps" (UNITS.size+1).times do |round| return "%.2f%s" % [bps, unit] if bps.abs < 1000 unit = UNITS[round] bps /= 1000 end end