Java File Size Readable Format formatByteSize(long bytes, int decimals)

Here you can find the source of formatByteSize(long bytes, int decimals)

Description

format Byte Size

License

Mozilla Public License

Declaration

public static String formatByteSize(long bytes, int decimals) 

Method Source Code

//package com.java2s;
/******************************************************************************
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0.  If a copy of the MPL was not distributed with this file,
 * You can obtain one at http://mozilla.org/MPL/2.0/.
 * //www. j a  va2  s  .  c o m
 * Software distributed under the License is distributed on an "AS IS" basis, 
 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for 
 * the specific language governing rights and limitations under the License.
 *
 * The Original Code is: FetchMailAtt
 * The Initial Developer of the Original Code is: William Wong (williamw520@gmail.com)
 * Portions created by William Wong are Copyright (C) 2015 William Wong, All Rights Reserved.
 *
 ******************************************************************************/

public class Main {
    public static String formatByteSize(long bytes, int decimals) {
        decimals = Math.min(Math.max(decimals, 0), 4);
        if (bytes < 1024)
            return bytes + "B";
        int exp = (int) (Math.log(bytes) / Math.log(1024));
        String pre = "KMGTPE".charAt(exp - 1) + "";
        return String.format("%." + decimals + "f%s", bytes / Math.pow(1024, exp), pre);
    }
}

Related

  1. convertFileSize(double size, Locale locale)
  2. convertFileSize(long size)
  3. convertFileSize(long size)
  4. formatarNumeroComZerosAEsquerda(long numero, int size)
  5. formatByteSize(long bytes, int lastDigits)
  6. formatByteSize(long byteSize)
  7. formatByteSizeToString(long bytes)
  8. formatDataSize(long size)