文章

Android storage存储单位转换(G M K B)

G M K B 单位之间转换,直接上代码

    // storage, G M K B
    public static String convertStorage(long size) {
        long kb = 1024;
        long mb = kb << 10;
        long gb = mb << 10;

        if (size >= gb) {
            return String.format("%.1f GB", (float) size / gb);
        } else if (size >= mb) {
            float f = (float) size / mb;
            return String.format(f > 100 ? "%.0f MB" : "%.1f MB", f);
        } else if (size >= kb) {
            float f = (float) size / kb;
            return String.format(f > 100 ? "%.0f KB" : "%.1f KB", f);
        } else
            return String.format("%d B", size);
    }

 

发表评论