get Scaling Unit Byte Size - Android java.lang

Android examples for java.lang:Byte

Description

get Scaling Unit Byte Size

Demo Code


import android.annotation.TargetApi;
import android.os.Build;
import android.os.SystemClock;
import java.io.File;
import java.math.RoundingMode;
import java.text.DecimalFormat;
import java.text.DecimalFormatSymbols;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;

public class Main{
    public static String getScalingUnitByteSize(int byteSize) {
        double scaledByteSize = (double) byteSize;
        String unit = "byte";

        if (scaledByteSize < 1024) {
            return formatNum(scaledByteSize, "0.##") + unit;
        } else {/*w w w  . j a v  a2  s  .  c  o m*/
            unit = "KiB";
            scaledByteSize /= 1024d;

            if (scaledByteSize < 1024) {
                return formatNum(scaledByteSize, "0.##") + unit;
            } else {
                unit = "MiB";
                scaledByteSize /= 1024d;
                if (scaledByteSize < 1024) {
                    return formatNum(scaledByteSize, "0.##") + unit;
                } else {
                    unit = "GiB";
                    scaledByteSize /= 1024d;
                    return formatNum(scaledByteSize, "0.##") + unit;
                }
            }
        }

    }
    public static String formatNum(double number) {
        return format.format(number);
    }
    public static String formatNum(double number, String formatString) {
        final DecimalFormat format = new DecimalFormat(formatString);
        format.setDecimalFormatSymbols(DecimalFormatSymbols
                .getInstance(Locale.ENGLISH));
        format.setRoundingMode(RoundingMode.HALF_UP);
        return format.format(number);
    }
}

Related Tutorials