Java Size readableSize(long size)

Here you can find the source of readableSize(long size)

Description

Return human readable file size.

License

Open Source License

Parameter

Parameter Description
size a parameter

Return

a human-readable display value (includes units - EB, PB, TB, GB, MB, KB or bytes).

Declaration

public static String readableSize(long size) 

Method Source Code


//package com.java2s;
//License from project: Open Source License 

import java.text.DecimalFormat;

public class Main {
    /**//from   ww  w.  j a  v  a  2s  .c o m
     * Return human readable file size. This function is limited to exabyte.
     * Note: How to handle overflow or underflow of long?
     * Source: http://stackoverflow.com/questions/3263892/format-file-size-as-mb-gb-etc
     * 
     * @param size
     * @return a human-readable display value (includes units - EB, PB, TB, GB, MB, KB or bytes).
     */
    public static String readableSize(long size) {
        if (size <= 1)
            return size + " byte";
        final String[] units = new String[] { "bytes", "KB", "MB", "GB", "TB", "PB", "EB" };
        int digitGroups = (int) (Math.log10(size) / Math.log10(1024));
        return new DecimalFormat("#,##0.##").format(size / Math.pow(1024, digitGroups)) + " " + units[digitGroups];
    }
}

Related

  1. getStringSize(String string)
  2. padString(String stringToPad, int size)
  3. read(byte[] input, int size)
  4. readableBytes(long byteSize)
  5. readableSize(long num)
  6. readableSize(long size)
  7. readableSize(long size, DecimalFormat format)
  8. renderNumber(int n, int size)
  9. size(long l)