Java Size getSizeString(long size)

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

Description

get Size String

License

Apache License

Declaration

public static String getSizeString(long size) 

Method Source Code

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

import java.text.DecimalFormat;

public class Main {
    private static final long KSIZE = 1024L;
    private static final long MSIZE = KSIZE * 1024L;
    private static final long GSIZE = MSIZE * 1024L;
    private static final long TSIZE = GSIZE * 1024L;

    public static String getSizeString(long size) {
        if (size < MSIZE) {
            DecimalFormat format = new DecimalFormat("0.0KB");
            return format.format((double) size / KSIZE);
        }//from   w  ww.  j  a v  a 2 s .  co  m
        if (size < GSIZE) {
            DecimalFormat format = new DecimalFormat("0.0MB");
            return format.format((double) size / MSIZE);
        }
        if (size < TSIZE) {
            DecimalFormat format = new DecimalFormat("0.0GB");
            return format.format((double) size / GSIZE);
        }
        DecimalFormat format = new DecimalFormat("0.0TB");
        return format.format((double) size / TSIZE);
    }
}

Related

  1. getSizeAll(final long bytes)
  2. getSizeFormat()
  3. getSizeInKB(String size)
  4. getSizeString(long bytes)
  5. getSizeString(long number, Locale locale)
  6. getSizeText(long size)
  7. getSpaceSizeFromByte(long xB)
  8. getStringSize(String string)
  9. padString(String stringToPad, int size)