Printing human-readable number of bytes
Possible solution: reduce your input to required precision. Returned result construction also reads cleaner with this approach:

def as_size(s)
  units = %W(B KiB MiB GiB TiB)

  size, unit = units.reduce(s.to_f) do |(fsize, _), utype|
    fsize &gt; 512 ? [fsize / 1024, utype] : (break [fsize, utype])
  end

  "#{size &gt; 9 || size.modulo(1) &lt; 0.1 ? '%d' : '%.1f'} %s" % [size, unit]
end