title: File size in human readable format
body: <p>Given the size of a file in bytes, I want to format it with <strong><a href="https://en.wikipedia.org/wiki/Binary_prefix" rel="noreferrer">IEC (binary) prefixes</a></strong> to <strong>3 <a href="http://en.wikipedia.org/wiki/Significant_figures" rel="noreferrer">significant figures</a> with trailing zeros</strong>, e.g. 1883954 becomes 1.80M.</p>
  
  <p>Floating-point arithmetic isn't supported in bash, so I used awk instead. The problem is I don't how to keep the trailing zeros. Current solution:</p>
  
  <pre><code>if [ $size -ge 1048576 ]
  then
      size=$(awk 'BEGIN {printf "%.3g",'$size'/1048576}')M
  elif [ $size -ge 1024 ]
  then
      size=$(awk 'BEGIN {printf "%.3g",'$size'/1024}')K
  fi
  </code></pre>
  
  <p>(The files aren't that big so I don't have to consider bigger units.)</p>
  
  <p>Edit: There's another problem with this. See Adrian Frühwirth's comment below.</p>