format long value in Byte, B, KB, MB, GM - Android java.lang

Android examples for java.lang:long

Description

format long value in Byte, B, KB, MB, GM

Demo Code

import android.util.Log;
import java.io.File;
import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

public class Main{

    public static String formatByte(long size) {

        if (size < BSize)
            return size + " B";
        if (1024 <= size && size < 1024 * 1024) {
            float result = size / (float) BSize;

            return df.format(result) + " KB";
        }//from   w ww .j  a  va 2s  .co m
        if (size >= KBSize && size < MBSize) {
            float result = size / (float) KBSize;

            return df.format(result) + " MB";
        }
        if (size >= MBSize) {
            float result = size / (float) MBSize;
            return df.format(result) + " GB";
        }
        return "";

    }

}

Related Tutorials