get long value in Size String using Math.round - Android java.lang

Android examples for java.lang:Math Trigonometric Function

Description

get long value in Size String using Math.round

Demo Code

import android.text.TextUtils;
import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.regex.PatternSyntaxException;

public class Main{

    public static String getSizeStr(long fileLength) {
        String strSize = "";
        try {//from w  ww  .  j  a v a 2 s . co m
            if (fileLength >= 1024 * 1024 * 1024) {
                strSize = (float) Math.round(10 * fileLength
                        / (1024 * 1024 * 1024))
                        / 10 + " GB";
            } else if (fileLength >= 1024 * 1024) {
                strSize = (float) Math.round(10 * fileLength
                        / (1024 * 1024 * 1.0))
                        / 10 + " MB";
            } else if (fileLength >= 1024) {
                strSize = (float) Math.round(10 * fileLength / (1024)) / 10
                        + " KB";
            } else if (fileLength >= 0) {
                strSize = fileLength + " B";
            } else {
                strSize = "0 B";
            }
        } catch (Exception e) {
            e.printStackTrace();
            strSize = "0 B";
        }
        return strSize;
    }

}

Related Tutorials