美文网首页Java学习笔记Java 杂谈
Java格式化数据量显示

Java格式化数据量显示

作者: learningops | 来源:发表于2018-03-22 12:59 被阅读18次
    /**
     * 格式化文件大小
     * 参考:https://stackoverflow.com/a/5599842/1253611
     *
     * @param size byte
     * @return readable file size
     */
    public static String readableFileSize(long size) {
        if (size <= 0) return "0";
        final String[] units = new String[]{"B", "kB", "MB", "GB", "TB"};
        int digitGroups = (int) (Math.log10(size) / Math.log10(1024));
        return new DecimalFormat("#,##0.#").format(size / Math.pow(1024, digitGroups)) + " " + units[digitGroups];
    }

相关文章

网友评论

    本文标题:Java格式化数据量显示

    本文链接:https://www.haomeiwen.com/subject/shkaqftx.html