code: public static String fileSizeAsStr(long size) {
          
          BigDecimal bd = BigDecimal.valueOf(size);
          if (size < 1024) {
              if (size == 1) {
                  return "1 byte";
              } else {
                  return size + " bytes";
              }
          }
          if (size < 1048576) {   // 1 MB
              return bd.divide(BDEC_KB, 0 , RoundingMode.HALF_UP).toPlainString() + " KB";
          }
          if (size < 1073741824) {   // 1 GB
              return bd.divide(BDEC_MB, 1 , RoundingMode.HALF_UP).toPlainString() + " MB";
          }
          return bd.divide(BDEC_GB, 2 , RoundingMode.HALF_UP).toPlainString() + " GB";
      }
docs: Formats a byte size (for example the size of a file) into an easy-to-read
  string. It is somewhat similar to the {@code -h} option which exist on
  many Unix/Linux commands. Readability is favored over precision.
  
  <p>
  Note that the method uses the abbreviations "KB", "MB" and "GB" to denote
  1024 bytes, 1024<sup>2</sup> bytes and 1024<sup>3</sup> bytes. Strictly
  speaking this is incorrect but this is what most operating systems do as well.
  
  <p>
  Rules are as follows:<br>
  If the size is &lt;1 KB : Will be shown as-is. Example returned string:
  <code>"361 bytes"</code><br>
  If the size is &gt;= 1 KB and &lt; 1 MB : Will be shown as KB value with
  no decimals. Example returned string: <code>"76 KB"</code><br>
  If the size is &gt;= 1 MB and &lt; 1 GB : Will be shown as MB value with
  1 decimal. Example returned string: <code>"323.4 MB"</code><br>
  If the size is &gt;= 1 GB : Will be shown as GB value with 2 decimal.
  Example returned string: <code>"1.42 GB"</code>
  
  <p>
  Except for input values above 1 TB the returned string will never be more
  than 9 bytes in length.
  
  @param size byte size
  @return formatted string