把byte转化为KB、MB、GB

/**
* @Description :

把byte转化为KB、MB、GB


**/

public static String getNetFileSizeDescription(long size) {
    StringBuffer bytes = new StringBuffer();
    DecimalFormat format = new DecimalFormat("###.0");
    if (size >= 1024 * 1024 * 1024) {
        double i = (size / (1024.0 * 1024.0 * 1024.0));
        bytes.append(format.format(i)).append("TB");
    }
    else if (size >= 1024 * 1024) {
        double i = (size / (1024.0 * 1024.0));
        bytes.append(format.format(i)).append("GB");
    }
    else if (size >= 1024) {
        double i = (size / (1024.0));
        bytes.append(format.format(i)).append("MB");
    }
    else if (size < 1024) {
        if (size <= 0) {
            bytes.append("0KB");
        }
        else {
            bytes.append((int) size).append("KB");
        }
    }
    return bytes.toString();
}
posted @ 2021-07-23 14:37  仲秋呀  阅读(1469)  评论(0)    收藏  举报