Printing human-readable number of bytes def as_size( s )
  prefix = %W(TiB GiB MiB KiB B)
  s = s.to_f
  i = prefix.length - 1
  while s &gt; 512 &amp;&amp; i &gt; 0
    s /= 1024
    i -= 1
  end
  ("%#{s &gt; 9 ? 'd' : '.1f'} #{prefix[i]}" % s).gsub /\.0/, ''
end


I'd like a clean and fast code to convert raw file size to human-readable format. Any hints?

Update (faster format):

def as_size( s )
  prefix = %W(TiB GiB MiB KiB B)
  s = s.to_f
  i = prefix.length - 1
  while s &gt; 512 &amp;&amp; i &gt; 0
    s /= 1024
    i -= 1
  end
  ((s &gt; 9 || s.modulo(1) &lt; 0.1 ? '%d' : '%.1f') % s) + ' ' + prefix[i]
end


Update (freeze the constant):

PREFIX = %W(TiB GiB MiB KiB B).freeze

def as_size( s )
  s = s.to_f
  i = PREFIX.length - 1
  while s &gt; 512 &amp;&amp; i &gt; 0
    i -= 1
    s /= 1024
  end
  ((s &gt; 9 || s.modulo(1) &lt; 0.1 ? '%d' : '%.1f') % s) + ' ' + PREFIX[i]
end
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