字节换算单位

来源:互联网 发布:广州服务器数据恢复 编辑:程序博客网 时间:2024/05/01 02:50

这是国处牛人写的一个比较牛的算法,收藏下

public static String humanReadableByteCount(long bytes) {
        int unit = 1024;
        if (bytes < unit) return bytes + " B";
        int exp = (int) (Math.log(bytes) / Math.log(unit));
        System.out.println("exp = "+exp);
        String pre = "KMGTPE".charAt(exp-1) + "";
        System.out.println("pre = "+pre);
        return String.format("%.1f %sB", bytes / Math.pow(unit, exp), pre);
    }

0 0
原创粉丝点击