Java Size Format formatSize(long bytes)

Here you can find the source of formatSize(long bytes)

Description

format Size

License

Apache License

Declaration

public static String formatSize(long bytes) 

Method Source Code


//package com.java2s;
//License from project: Apache License 

import java.text.DecimalFormat;

public class Main {
    public static long KB = 1024;
    public static long MB = 1024 * KB;
    public static long GB = 1024 * MB;
    public static long TB = 1024 * GB;

    public static String formatSize(long bytes) {
        DecimalFormat df = new DecimalFormat(".0");
        if (bytes > TB)
            return df.format(bytes * 1.0d / TB) + "TB";
        if (bytes > GB)
            return df.format(bytes * 1.0d / GB) + "GB";
        if (bytes > MB)
            return df.format(bytes * 1.0d / MB) + "MB";
        if (bytes > KB)
            return df.format(bytes * 1.0d / KB) + "KB";
        return bytes + "B";
    }//from  www.ja  v a2 s  . com
}

Related

  1. formatSize(double fileSize)
  2. formatSize(double size)
  3. formatSize(int size)
  4. formatSize(long bytes)
  5. formatSize(long bytes)
  6. formatSize(long fileSize)
  7. formatSize(long longSize)
  8. formatSize(long longSize, int decimalPos)
  9. formatSize(long size)