Java Size bytesToUnits(long size)

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

Description

bytes To Units

License

Apache License

Declaration

public static String bytesToUnits(long size) 

Method Source Code

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

import java.text.DecimalFormat;

public class Main {
    public static String bytesToUnits(long size) {
        String hrSize = null;/*from   www  . j a v  a  2 s  . c o  m*/

        double b = size;
        double k = size / 1024.0;
        double m = ((size / 1024.0) / 1024.0);
        double g = (((size / 1024.0) / 1024.0) / 1024.0);
        double t = ((((size / 1024.0) / 1024.0) / 1024.0) / 1024.0);

        DecimalFormat dec = new DecimalFormat("0.00");

        if (t > 1) {
            hrSize = dec.format(t).concat(" TB");
        } else if (g > 1) {
            hrSize = dec.format(g).concat(" GB");
        } else if (m > 1) {
            hrSize = dec.format(m).concat(" MB");
        } else if (k > 1) {
            hrSize = dec.format(k).concat(" KB");
        } else {
            hrSize = dec.format(b).concat(" Bytes");
        }

        return hrSize;
    }
}

Related

  1. byteCountToDisplaySize(long size)
  2. byteSizeString(long bytes)
  3. BytesToHRSize(String len)
  4. bytesToSizeFormat(long bytes)
  5. bytesToString(long sizeInBytes)
  6. calcSize(double s, String type, int div)
  7. createSizeFormat(Locale locale)
  8. getBytesSpecificationDescription(double sizeInBytes, String formatSpec)
  9. getByteStr(long size)